因此,您可以使用AsynchronousFileChannel将整个文件读入字符串:
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(filePath, StandardOpenOption.READ);
long len = fileChannel.size();
ReadAttachment readAttachment = new ReadAttachment();
readAttachment.byteBuffer = ByteBuffer.allocate((int) len);
readAttachment.asynchronousChannel = fileChannel;
CompletionHandler<Integer, ReadAttachment> completionHandler = new CompletionHandler<Integer, ReadAttachment>() {
@Override
public void completed(Integer result, ReadAttachment attachment) {
String content = new String(attachment.byteBuffer.array());
try {
attachment.asynchronousChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
completeCallback.accept(content);
}
@Override
public void failed(Throwable exc, ReadAttachment attachment) {
exc.printStackTrace();
exceptionError(errorCallback, completeCallback, String.format("error while reading file [%s]: %s", path, exc.getMessage()));
}
};
fileChannel.read(
readAttachment.byteBuffer,
0,
readAttachment,
completionHandler);假设现在,我不想分配整个ByteBuffer,而是逐行读取。我可以使用一个固定宽度的ByteBuffer,并多次回忆read,总是复制和附加到一个StringBuffer,直到我没有到新的行.我唯一关心的是:因为我正在读取的文件的编码可能是每个字符的多字节(UTF之类的),所以读取的字节可能以一个不完整的字符结束。如何确保将正确的字节转换为字符串,而不破坏编码?
更新:答案在所选答案的注释中,但它基本上指向CharsetDecoder。
发布于 2015-10-19 15:20:39
如果您有明确的ASCII分隔符(在您的情况下是(\n) ),那么您不需要关心不完整的字符串,因为这个字符映射到单字节(反之亦然)。
因此,只需在输入中搜索'\n‘字节,然后读取并将之前的任何内容转换为字符串。循环,直到找不到新的行为止。然后压缩缓冲区并重新使用它进行下一次读取。如果找不到新行,就必须分配更大的缓冲区,复制旧缓冲区的内容,然后再调用read。
编辑:正如注释中提到的,您可以动态地将ByteBuffer传递给CharsetDecoder,并将其转换为CharBuffer (然后附加到StringBuilder或其他预置的解决方案)。
发布于 2015-10-19 15:43:30
试试扫描仪:
Scanner sc = new Scanner(FileChannel.open(filePath, StandardOpenOption.READ));
String line = sc.readLine();FileChannel是InterruptibleChannel
https://stackoverflow.com/questions/33198088
复制相似问题