我是php开发,最近因为项目需要,需临时学习java开发。读取文件,是在项目中很重要的功能,同时也是我练手的第一个功能。下面的函数实现了按行读取文件内容,并将行数记录功能。
- 代码
public static int readFileLineCounts() throws IOException{
int count = 0;
File file = new File("/tmp/test.log");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
while((br.readLine())!=null){
count++;
}
return count;
}