io 缓冲读写与非缓冲 读写性能对照

io 缓冲读写与非缓冲 读写性能对比

package com.cn.ld.exercises.file;

 

import java.io.BufferedInputStream;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

 

public class FileRWPerformance {

private static void writeFileToDiskFW() throws IOException {

Long l1 = System.currentTimeMillis() ;

String str = "aaaaaaaaaa" ;

   FileWriter  fw = new FileWriter(new File("d:"+File.pathSeparator+"buferOper.txt"));

   for(int i = 300000 ; i>0 ; i--){

    fw.write(str);

   }

   Long l2 = System.currentTimeMillis() ;

   

   System.out.println(Thread.currentThread().getStackTrace()[2].getMethodName()+" total time:"+(l2-l1));

}

private static void writeFileToDiskFWandBuf() throws IOException {

Long l1 = System.currentTimeMillis() ;

String str = "aaaaaaaaaa" ;

   FileWriter  fw = new FileWriter(new File("d:"+File.pathSeparator+"buferOper.txt"));

   BufferedWriter  bw =  new BufferedWriter(fw);

   for(int i = 300000 ; i>0 ; i--){

    bw.write(str);

   }

   Long l2 = System.currentTimeMillis() ;

   System.out.println(Thread.currentThread().getStackTrace()[2].getMethodName()+"total time:"+(l2-l1));

}

 

private static void ReadFileFromDisk() throws IOException {

Long l1 = System.currentTimeMillis() ;

File  f = new File("d:"+File.pathSeparator+"buferOper.txt");

byte [] b = new byte[1024];

InputStream is  = null ;

int length = 0 ;

try {

is = new FileInputStream(f);

while( (length = is.read(b)) != -1){

;//System.out.println(b.toString());

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}finally{

is.close();

}

Long l2 = System.currentTimeMillis() ;

System.out.println("ReadFileFromDisk total time:"+(l2-l1));

}

private static void ReadFileFromMemory() throws IOException {

Long l1 = System.currentTimeMillis() ;

File  f = new File("d:"+File.pathSeparator+"buferOper.txt");

byte [] b = new byte[1024];

BufferedInputStream bs  = null ;

int length = 0 ;

try {

bs = new BufferedInputStream(new FileInputStream(f));

while( (length = bs.read(b)) != -1){

;//System.out.println(b.toString());

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}finally{

bs.close();

}

Long l2 = System.currentTimeMillis() ;

System.out.println(Thread.currentThread().getStackTrace()[2].getMethodName()+"total time:"+(l2-l1));

}

public static void main(String[] args) {

try {

for(int i = 100; i>0 ; i--){

FileRWPerformance.writeFileToDiskFW();

           FileRWPerformance.writeFileToDiskFWandBuf();

           

           FileRWPerformance.ReadFileFromDisk();

           

           FileRWPerformance.ReadFileFromMemory() ;

           

           System.out.println("----------------"+i+"------------------");

} catch (IOException e) {

e.printStackTrace();

}

}

}



writeFileToDiskFW total time:125
writeFileToDiskFWandBuftotal time:32
ReadFileFromDisk total time:15
ReadFileFromMemorytotal time:0