我有寫代碼來打印文本文件的全部文本,但我可以不知道如何使它能夠閱讀全文除了最後一行如何讀取除最後一行以外的文本文件中的整個文本?
驗證碼:
public class Files {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// -- This Code is to print the whole text in text file except the last line >>>
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("FileToPrint.txt"));
String s = br.readLine();
while (true) {
if ((sCurrentLine = br.readLine()) != null) {
System.out.println(s);
s = sCurrentLine;
}
if ((sCurrentLine = br.readLine()) != null) {
System.out.println(s);
s = sCurrentLine;
} else {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
我想上面的代碼可以讀除了最後一行文字,,,
感謝您的幫助
你爲什麼在循環中讀兩行? –