一、直接读取
public static void read1(File file)throws Exception{
FileInputStream fis = new FileInputStream(file);
int b = 0;
int count = 1;
while((b = fis.read()) != -1){
if(b == '\n')
count++;
}
fis.close();
System.out.println(count);
}
二、全部读到缓冲区,在读取
public staticvoid read2(File file){
try{
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int b = 0;
int count = 1;
while((b = bis.read())!=-1)
if(b == '\n')
count++;
fis.close();
bis.close();
System.out.println(count);
}catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
}
三、读一部分到缓冲区,再读取。如此往复
public static void read3(File file) throws Exception{
FileInputStream fis = new FileInputStream(file);
byte []buf = new byte[1024];
int b = 0;
int count = 1;
while((b = fis.read(buf) )!=-1)
if(b == '\n')
count++;
fis.close();
System.out.println(count);
}