我需要修改一个文件。我们已经编写了一个相当复杂的组件来构建索引集,这些索引描述了这个文件中有趣的东西,但是现在我需要使用那组索引来编辑这个文件,这证明是困难的。
具体来说,我的梦想API是这样的
//if you'll let me use kotlin for a second, assume we have a simple tuple class
data class IdentifiedCharacterSubsequence { val indexOfFirstChar : int, val existingContent : String }
//given these two structures
List<IdentifiedCharacterSubsequences> interestingSpotsInFile = scanFileAsPerExistingBusinessLogic(file, businessObjects);
Map<IdentifiedCharacterSubsequences, String> newContentByPreviousContentsLocation = generateNewValues(inbterestingSpotsInFile, moreBusinessObjects);
//I want something like this:
try(MutableFile mutableFile = new com.maybeGoogle.orApache.MutableFile(file)){
for(IdentifiedCharacterSubsequences seqToReplace : interestingSpotsInFile){
String newContent = newContentByPreviousContentsLocation.get(seqToReplace);
mutableFile.replace(seqToReplace.indexOfFirstChar, seqtoReplace.existingContent.length, newContent);
//very similar to StringBuilder interface
//'enqueues' data changes in memory, doesnt actually modify file until flush call...
}
mutableFile.flush();
// ...at which point a single write-pass is made.
// assumption: changes will change many small regions of text (instead of large portions of text)
// -> buffering makes sense
}
一些注意事项:
RandomAccessFile
因为我的更改未就位(newContent
的长度可能比seq.existingContent
更长或更短)这样的事情是存在还是我被简化为使用BufferedWriters等编写我自己的实现?对于一种通常强调基于索引的行为的语言来说,io.Streams
似乎是一种明显的演变,但我无法找到现有的实现。
最后:我对文件和编码方案的域名经验很少,所以我不遗余力地解决这些问题中描述的“双索引”字符:Java charAt used with characters that have two code units。在这方面的任何帮助非常感谢。这可能是我找不到这样的实现的原因吗?因为UTF-8编码文件中的索引是如此讨厌且容易出错?