1、文件行数的读取
方法1:BufferedReader.readLine(), 20000条数据经测试耗时16毫秒
public
int getValidateLineCount(String fileName) {
int lineCount = 0;
try {
FileInputStream fis = new FileInputStream( new File(fileName));
BufferedReader br = new BufferedReader( new InputStreamReader(fis));
while (br.readLine() != null) {
lineCount++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return lineCount;
}
int lineCount = 0;
try {
FileInputStream fis = new FileInputStream( new File(fileName));
BufferedReader br = new BufferedReader( new InputStreamReader(fis));
while (br.readLine() != null) {
lineCount++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return lineCount;
}
方法2:DataInputStream.readLine(), 但是readLine()方法已不建议使用,且在本次实践中测试效率较低。经测试20000条数据耗时:3688毫秒
public
int getValidateLineCount(String fileName) {
int lineCount = 0;
try {
FileInputStream fis = new FileInputStream( new File(fileName));
DataInputStream dis = new DataInputStream(fis);
while (dis.readLine() != null) {
lineCount++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return lineCount;
}
int lineCount = 0;
try {
FileInputStream fis = new FileInputStream( new File(fileName));
DataInputStream dis = new DataInputStream(fis);
while (dis.readLine() != null) {
lineCount++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return lineCount;
}
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/280443,如需转载请自行联系原作者