Efficiency of writing Java Io files-comparison of several methods

Source: Internet
Author: User

Test write class

 

/**
* Test results
*
* 1. File's long: 16 KB
*
Filewrite's time ---------- 36
Outputstream test's time ---------- 167
Bufferedoutputtest's time ---------- 17
Bufferedwritetest's time ---------- 14
Bufferedwrite and filewritertest's time ---------- 9
Bufferedwrite and bufferedoutputstreamtest's time ---------- 12
*
* 2. File's long: 1600kb
Filewrite's time ---------- 69
Outputstream test's time ---------- 1282
Bufferedoutputtest's time ---------- 68
Bufferedwritetest's time ---------- 40
Bufferedwrite and filewritertest's time ---------- 52
Bufferedwrite and bufferedoutputstreamtest's time ---------- 37
*
* 3. File's long: 16000kb
Filewrite's time ---------- 555
Outputstream test's time ---------- 12448
Bufferedoutputtest's time ---------- 599
Bufferedwritetest's time ---------- 346
Bufferedwrite and filewritertest's time ---------- 316
Bufferedwrite and bufferedoutputstreamtest's time ---------- 358

4. File's long: 16.0kb

Filewrite's time ---------- 5203
Outputstream test's time ---------- 127182
Bufferedoutputtest's time ---------- 5972
Bufferedwritetest's time ---------- 3445 optimal
Bufferedwrite and filewritertest's time ---------- 5904
Bufferedwrite and bufferedoutputstreamtest's time ---------- 5353


5. File's long: 1600000kb

Filewrite's time ---------- 50416
Outputstream test's time ---------- 1303242
Bufferedoutputtest's time ---------- 60931
Bufferedwritetest's time ---------- 46697
Bufferedwrite and filewritertest's time ---------- 48710
Bufferedwrite and bufferedoutputstreamtest's time ---------- 64354
*/

Public static void main (string [] ARGs ){

String STR = "abcdefghijklmn! ";
Int COUNT = 1000000;
Testoutputstream T = new testoutputstream ();
Long start = system. currenttimemillis ();
T. filewritetest (count, STR );
Long end = system. currenttimemillis ();
System. Out. println ("filewrite's time ---------" + (START-end ));

Start = system. currenttimemillis ();
T. outputstreamtest (count, STR );
End = system. currenttimemillis ();
System. Out. println ("outputstreamtest's time ---------" + (START-end ));

Start = system. currenttimemillis ();
T. bufferedoutputtest (count, STR );
End = system. currenttimemillis ();
System. Out
. Println ("bufferedoutputtest's time ---------" + (START-end ));

Start = system. currenttimemillis ();
T. bufferedwritetest (count, STR );
End = system. currenttimemillis ();
System. Out. println ("bufferedwritetest's time ---------" + (START-end ));

Start = system. currenttimemillis ();
T. bufferedwriteandfilewritertest (count, STR );
End = system. currenttimemillis ();
System. Out. println ("bufferedwrite and filewritertest's time ---------" + (START-end ));

Start = system. currenttimemillis ();
T. bufferedwriteandbufferedoutputstreamtest (count, STR );
End = system. currenttimemillis ();
System. Out. println ("bufferedwrite and bufferedoutputstreamtest's time ---------" + (START-end ));



}
 

/**
* 1 Write fileoutputstream by byte
*
* @ Param count Number of Write cycles
* @ Param STR write a string
*/
Public void outputstreamtest (INT count, string Str ){
File F = new file ("F: test1.txt ");
Outputstream OS = NULL;
Try {
OS = new fileoutputstream (f );
For (INT I = 0; I <count; I ++ ){
OS. Write (Str. getbytes ());
}
OS. Flush ();
System. Out. println ("file's long:" + F. Length ());
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
Try {
OS. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}

/**
* 2 Write bufferedoutputstream in byte Buffer
*
* @ Param count Number of Write cycles
* @ Param STR write a string
*/
Public void bufferedoutputtest (INT count, string Str ){
File F = new file ("F: test2.txt ");
Bufferedoutputstream Bos = NULL;
Try {
Outputstream OS = new fileoutputstream (f );
Bos = new bufferedoutputstream (OS );
For (INT I = 0; I <count; I ++ ){
Bos. Write (Str. getbytes ());
}
Bos. Flush ();
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
Try {
Bos. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}
 
/**
* 3 Write filewriter by character
*
* @ Param count Number of Write cycles
* @ Param STR write a string
*/
Public void filewritetest (INT count, string Str ){
File F = new file ("F: test.txt ");
Writer writer = NULL;
Try {
Writer = new filewriter (f );
For (INT I = 0; I <count; I ++ ){
Writer. Write (STR );
}
Writer. Flush ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
Try {
Writer. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}

/**
* 4 Write bufferedwriter by Character Buffer
*
* @ Param count Number of Write cycles
* @ Param STR write a string
*/
Public void bufferedwritetest (INT count, string Str ){
File F = new file ("F: test3.txt ");
Outputstreamwriter writer = NULL;
Bufferedwriter BW = NULL;
Try {
Outputstream OS = new fileoutputstream (f );
Writer = new outputstreamwriter (OS );
BW = new bufferedwriter (writer );
For (INT I = 0; I <count; I ++ ){
Bw. Write (STR );
}
Bw. Flush ();
If (F. exists ()){
F. Delete ();
}
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
Try {
Bw. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}
 
/**
* 5 Write bufferedwriter and bufferedoutputstream by Character Buffer
*
* @ Param count Number of Write cycles
* @ Param STR write a string
*/
Public void bufferedwriteandbufferedoutputstreamtest (INT count, string Str ){
File F = new file ("F: test4.txt ");
Bufferedoutputstream Bos = NULL;
Outputstreamwriter writer = NULL;
Bufferedwriter BW = NULL;
Try {
Outputstream OS = new fileoutputstream (f );
Bos = new bufferedoutputstream (OS );
Writer = new outputstreamwriter (BOS );
BW = new bufferedwriter (writer );
For (INT I = 0; I <count; I ++ ){
Bw. Write (STR );
}
Bw. Flush ();
If (F. exists ()){
F. Delete ();
System. Out. println ("delete ---");
}
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
Try {
Bw. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}
 
/**
* 6 Write bufferedwriter and filewriter by Character Buffer
*
* @ Param count Number of Write cycles
* @ Param STR write a string
*/
Public void bufferedwriteandfilewritertest (INT count, string Str ){
File F = new file ("F: test5.txt ");
Filewriter fw = NULL;
Bufferedwriter BW = NULL;
Try {
FW = new filewriter (f );
BW = new bufferedwriter (FW );
For (INT I = 0; I <count; I ++ ){
Bw. Write (STR );
}
Bw. Flush ();
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
Try {
Bw. Close ();
If (F. exists ()){
F. Delete ();
}
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}

 

Summary:

For classification by character and byte, except methods 1 and 2, the rest are written to files by character, and writing by character is generally faster than writing by byte. According to Java API, the parent class of filewriter is outputstreamwriter, both of them are writer classes. From this point of view, Method 4 and Method 6 are almost identical, and time is slightly different, but the internal mechanism is the same, and Method 6 is relatively simple, the variable definition is a little bit. In the past, Method 4 was used. It seems that it will be changed later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.