我想讀取一個ascii文件並識別換行符「\ n」的位置,以知道每行有哪些字符和多少個字符。文件大小爲538MB。當我運行下面的代碼時,它從來不打印任何東西。 我搜索了很多,但我沒有找到任何ascii文件。我使用NetBeans和Java 8.任何想法??逐行讀取ascii文件 - Java
以下是我的代碼。
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");
}
}
}
你看過http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file-in-java? –
我已經看到這篇文章,但與BufferReader它拋出我Java內存不足錯誤,所以我無法使用readline()函數。 – lostromos
對於大文件,使用'RandomAccessFile'而不是'FileReaders'。 – ccc