請教readline函數的用法?
readLine()是讀取流讀數據的時候用的,當讀到換行標記'\n'、'\r'(回車)時,會跟著換行,同時會以字符串形式返回這一行的數據,當讀取完所有的數據時會返回null
具體用法如下:
public static void readFileBylines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行為單位讀取文件內容,一次讀一行");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
//一次讀一行,讀入null時文件結束
while ((tempString = reader.readLine()) != null) {
//把當前行號顯示出來
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}