【发布时间】:2016-01-24 14:21:52
【问题描述】:
我正在尝试读取一个 ascii 文件并识别换行符“\n”的位置,以了解每行中有哪些字符和多少个字符。文件大小为 538MB。当我运行下面的代码时,它从不打印任何东西。 我搜索了很多,但我没有找到任何 ascii 文件。我使用 netbeans 和 Java 8。有什么想法吗??
下面是我的代码。
String inputFile = "C:\myfile.txt";
FileInputStream in = new FileInputStream(inputFile);
FileChannel ch = in.getChannel();
int BUFSIZE = 512;
ByteBuffer buf = ByteBuffer.allocateDirect(BUFSIZE);
Charset cs = Charset.forName("ASCII");
while ( (rd = ch.read( buf )) != -1 ) {
buf.rewind();
CharBuffer chbuf = cs.decode(buf);
for ( int i = 0; i < chbuf.length(); i++ ) {
if (chbuf.get() == '\n'){
System.out.println("PRINT SOMETHING");
}
}
}
【问题讨论】:
-
我已经看过这篇文章,但是使用 BufferReader 它会抛出 Java Out of Memory 错误,所以我无法使用 readline() 函数。
-
对大文件使用
RandomAccessFile而不是FileReaders。