繁体   English   中英

从巨大的文件中读取换行符隔开的MemoryMapped文件

[英]MemoryMapped File reading from a huge file separated by new line character

我正在使用内存映射的I / O读取巨大的文件。 我遇到的问题是,我正在逐字符读取MemoryMappedByteBuffer。 因此,我需要传递文件中存在的多个字符串,这些字符串之间用“ \\ n”分隔。

        RandomAccessFile aFile = new RandomAccessFile(this.getFileName(), "r");
        FileChannel inChannel = aFile.getChannel();
        MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 
        0, inChannel.size());

        buffer.load(); 
        for (int i = 0; i < buffer.limit(); i++)
        {   // There are many strings in the file separated by \n
            System.out.println((char) buffer.get() == '\n'); // Gives true 
             //need to make a complete string over here.
        }
        buffer.clear(); // do something with the data and clear/compact it.           
        return null; // The String which has been made in the above for loop

这不是您要的答案,因为它不使用内存映射文件。 如果确实没有帮助,我将其删除。

我正在使用内存映射的I / O读取巨大的文件

如果文件确实很大,那么您采用的方法将需要大量内存。

一种替代方法是使用BufferedReader ,这使您的任务变得微不足道:

final List<String> lines = new ArrayList<>();
final BufferedReader br = new BufferedReader(
    new InputStreamReader(new FileInputStream(file), charset));

String line;
while ((line = br.readLine()) != null)
{
  lines.add(line);
}

return lines;

JDK实用程序方法Files.readAllLines(Path,Charset)已经包含与该代码等效的代码:

读取文件中的所有行。 此方法可确保在读取所有字节或引发I / O错误或其他运行时异常时关闭文件。 使用指定的字符集将文件中的字节解码为字符。

 public static List<String> readAllLines(Path path, Charset cs) throws IOException 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM