私はLineNumberReaderを使用してテキストファイルを読み取ります。setLineNumberとgetLinenumberを呼び出すとprint==0ですが、readLineをもう一度呼び出すと์Nitが発生します。どうすれば修正できますか。これが私のコードです。
BufferedWriter writer = new BufferedWriter(new FileWriter("text.txt"));
writer.write("This is a line1\n" +
"This is a line2\n" +
"This is a line3");
writer.newLine();
writer.close();
File myFile = new File("text.txt");
FileReader fileReader = new FileReader(myFile);
LineNumberReader reader = new LineNumberReader(fileReader);
// Read from the FileReader.
String lineRead = "";
while ((lineRead = reader.readLine()) != null) {
System.out.println(lineRead);
}
// Determine the number of lines that were read.
System.out.println("Total lines read: " +
reader.getLineNumber());
// Reset the number of lines read.
reader.setLineNumber(0);
System.out.println("Total lines read after reset: " +
reader.getLineNumber());
String lineRead2 = "";
while ((lineRead2 = reader.readLine()) != null) {
System.out.println(lineRead2);
}
System.out.println("End");
// Close the LineNumberReader and FileReader.
fileReader.close();
reader.close();
感謝