[英]MemoryMapped File reading from a huge file separated by new line character
I am reading a huge file using memory mapped I/O. 我正在使用内存映射的I / O读取巨大的文件。 The problem which i came across is, i am reading MemoryMappedByteBuffer character by character the . 我遇到的问题是,我正在逐字符读取MemoryMappedByteBuffer。 So i need to pass multiple strings present in the file which are separated by "\\n". 因此,我需要传递文件中存在的多个字符串,这些字符串之间用“ \\ 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
This is not the answer you're asking for, as this does not use a memory-mapped file. 这不是您要的答案,因为它不使用内存映射文件。 If it's truly not helpful, I'll remove it. 如果确实没有帮助,我将其删除。
I am reading a huge file using memory mapped I/O 我正在使用内存映射的I / O读取巨大的文件
If the file really is huge, then the approach you're taking is going to be quite memory demanding. 如果文件确实很大,那么您采用的方法将需要大量内存。
An alternative is to use a BufferedReader
, and this makes your task quite trivial: 一种替代方法是使用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;
An equivalent of this code is already included in the JDK utility method Files.readAllLines(Path,Charset)
: JDK实用程序方法Files.readAllLines(Path,Charset)
已经包含与该代码等效的代码:
Read all lines from a file. 读取文件中的所有行。 This method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown. 此方法可确保在读取所有字节或引发I / O错误或其他运行时异常时关闭文件。 Bytes from the file are decoded into characters using the specified charset. 使用指定的字符集将文件中的字节解码为字符。
public static List<String> readAllLines(Path path, Charset cs) throws IOException
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.