Unknown original address
From the old zizhu website
Import java. Io. bufferedoutputstream;
Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. filewriter;
Import java. NiO. bytebuffer;
Import java. NiO. channels. filechannel;
/**
* Test the performance of various file writing methods.
*
* @ Author old zizhu Java century network (java2000.net)
*
*/
Public class t {
Public static void main (string [] ARGs ){
Fileoutputstream out = NULL;
Fileoutputstream outstr = NULL;
BufferedOutputStream Buff = null;
FileWriter fw = null;
Int count = 1000000; // number of lines written to the file
Try {
Byte [] bs = "test java File Operations \ r \ n". getBytes ();
Out = new FileOutputStream (new File ("C:/add.txt "));
Long begin = System. currentTimeMillis ();
For (int I = 0; I <count; I ++ ){
Out. write (bs );
}
Out. close ();
Long end = System. currentTimeMillis ();
System. out. println ("FileOutputStream execution time:" + (end-begin) + "haoxi ");
OutSTr = new FileOutputStream (new File ("C:/add0.txt "));
Buff = new BufferedOutputStream (outSTr );
Long begin0 = System. currentTimeMillis ();
For (int I = 0; I <count; I ++ ){
Buff. write (bs );
}
Buff. flush ();
Buff. close ();
Long end0 = System. currentTimeMillis ();
System. out. println ("BufferedOutputStream execution time:" + (end0-begin0) + "haoxi ");
Fw = new FileWriter ("C:/add2.txt ");
Long begin3 = System. currentTimeMillis ();
For (int I = 0; I <count; I ++ ){
Fw. write ("test java File Operations \ r \ n ");
}
Fw. flush ();
Fw. close ();
Long end3 = System. currentTimeMillis ();
System. out. println ("FileWriter execution time:" + (end3-begin3) + "haoxi ");
Long begin4 = System. currentTimeMillis ();
String path = "C:/add3.txt ";
ByteBuffer bb = ByteBuffer. wrap (bs );
FileChannel out2 = new FileOutputStream (path). getChannel ();
For (int I = 0; I <count; I ++ ){
Out2.write (bb );
Bb. rewind ();
}
Out2.close ();
Long end4 = system. currenttimemillis ();
System. Out. println ("filechannel execution time:" + (end4-begin4) + "haoxi ");
} Catch (exception e ){
E. printstacktrace ();
} Finally {
Try {
FW. Close ();
Buff. Close ();
Outstr. Close ();
Out. Close ();
} Catch (exception e ){
E. printStackTrace ();
}
}
}
}
The running result on my notebook is as follows:
FileOutputStream execution Duration: 4891 seconds
BufferedOutputStream execution time: 78 Hao seconds
FileWriter execution time: 438 seconds
FileChannel execution time: 2812 seconds
Of course, the final file generated is correct.
Summary:
BufferedOutputStream has excellent performance due to buffering.
FileOutputStream has the worst performance because it writes bytes each time.
FileWriter has excellent performance.
FileChannel uses nio, but if there is no buffer, performance is expected.
The above is the reprinted content:
Test result: (run 3 times)
FileOutputStream execution Duration: 7035 seconds
BufferedOutputStream execution time: 100 seconds
FileWriter execution time: 439 seconds
FileChannel execution time: 8501 seconds
FileOutputStream execution Duration: 7001 seconds
BufferedOutputStream execution time: 96 Hao seconds
FileWriter execution time: 491 seconds
FileChannel execution time: 8162 seconds
FileOutputStream execution Duration: 7119 seconds
BufferedOutputStream execution time: 97 Hao seconds
FileWriter execution time: 416 seconds
FileChannel execution time: 8070 seconds
For more information, see http://zhangzcz%.iteye.com/blog/515121;