java大文件读取效率对比

分别采用三种方式读取大小为74GB共约6亿条记录的文件,BufferedReader 性能最好,RandomAccessFile最差,性能差距超过1百倍


public static void scannerReader(String filename){
File f = new File(filename);
if(f.exists() && f.isFile() && f.length()>0){
try {
long times=0;
long startTime = System.currentTimeMillis();
Scanner sc = new Scanner(f);
while(sc.hasNextLine()){
String temp = sc.nextLine();
times++;
}
sc.close();
long endTime = System.currentTimeMillis();
System.out.println("Scanner 执行时间:"+(endTime-startTime) +"当前输出第:"+times+"条数据");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void bufferReader(String filename) throws Exception{
BufferedReader reader = null;
try {
File read= FileUtils.getFile(filename);
reader = new BufferedReader(new FileReader(read.getAbsoluteFile()));
String line = null;
long times=0;
long startTime = System.currentTimeMillis();
while((line = reader.readLine()) != null) {
times++;
}
long endTime = System.currentTimeMillis();
System.out.println("BufferedReader 执行时间:"+(endTime-startTime) +"当前输出第:"+times+"条数据");
} catch (IOException e) {
e.printStackTrace();
}finally {
}
}
public static void ranrReader(String filename)throws Exception{
RandomAccessFile raf = null;
FileChannel fc = null;
try {
raf = new RandomAccessFile(filename, "r");
//raf.seek(0);
fc = raf.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(100000);
int readcount = -1;
long times=0;
long startTime = System.currentTimeMillis();
while ((readcount = fc.read(buffer)) != -1) {
String line = raf.readLine();
times++;
}
long endTime = System.currentTimeMillis();
System.out.println("RandomAccessFile 执行时间:"+(endTime-startTime) +"当前输出第:"+times+"条数据");
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}

BufferedReader 执行时间:244007当前输出第:600037902条数据
Scanner 执行时间:2219763当前输出第:600037902条数据
RandomAccessFile 执行时间:35246077当前输出第:600037902条数据

你可能感兴趣的:(java大文件读取效率对比)