首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用AsynchronousFileChannel有效地读取StringBuffer

如何使用AsynchronousFileChannel有效地读取StringBuffer
EN

Stack Overflow用户
提问于 2015-10-18 12:47:11
回答 2查看 853关注 0票数 5

因此,您可以使用AsynchronousFileChannel将整个文件读入字符串:

代码语言:javascript
复制
 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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-19 15:20:39

如果您有明确的ASCII分隔符(在您的情况下是(\n) ),那么您不需要关心不完整的字符串,因为这个字符映射到单字节(反之亦然)。

因此,只需在输入中搜索'\n‘字节,然后读取并将之前的任何内容转换为字符串。循环,直到找不到新的行为止。然后压缩缓冲区并重新使用它进行下一次读取。如果找不到新行,就必须分配更大的缓冲区,复制旧缓冲区的内容,然后再调用read。

编辑:正如注释中提到的,您可以动态地将ByteBuffer传递给CharsetDecoder,并将其转换为CharBuffer (然后附加到StringBuilder或其他预置的解决方案)。

票数 1
EN

Stack Overflow用户

发布于 2015-10-19 15:43:30

试试扫描仪:

代码语言:javascript
复制
    Scanner sc = new Scanner(FileChannel.open(filePath, StandardOpenOption.READ));
   String line = sc.readLine();

FileChannel是InterruptibleChannel

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33198088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档