Comparison of Java file writing methods

Source: Internet
Author: User

Keywords: Java File
Recently, we optimized a previously developed general data migration software. In addition to the improvements made by using jdk5.0's multithreading technology, it also compares the performance of writing files in Java.
Fileoutputstream and filewriter are usually used to write files in Java. filewriter can only write text files. Fileoutputstream is often combined with bufferedoutputstream. In actual applications, most text files are written. So the following test uses three different methods to generate a file of the same number of rows and the same size.

Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io .*;

Public class filetest {
Public filetest (){
}

Public static void main (string [] ARGs ){
Fileoutputstream out = NULL;
Fileoutputstream outstr = NULL;
Bufferedoutputstream buff = NULL;
Filewriter fw = NULL;
Int COUNT = 1000; // number of lines written to the file
Try {
Out = new fileoutputstream (new file ("C:/add.txt "));
Long begin = system. currenttimemillis ();
For (INT I = 0; I <count; I ++ ){
Out. Write ("test Java File Operations/R/N". getbytes ());
}
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 ("test Java File Operations/R/N". getbytes ());
}
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. Close ();
Long end3 = system. currenttimemillis ();
System. Out. println ("filewriter execution time:" + (end3-begin3) + "haoxi ");

} Catch (exception e ){
E. printstacktrace ();
}
Finally {
Try {
FW. Close ();
Buff. Close ();
Outstr. Close ();
Out. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}

}

The following results are executed several times and the common data is retrieved. Because they are simple and compared, no detailed statistics are made.

1. When Count = 1000, that is, when writing 1000 lines of a file, the size of the file written is 18.5kb:
Fileoutputstream execution time: 46 Hao seconds
Bufferedoutputstream execution time: 31 Hao seconds
Filewriter execution time: 15 seconds

2. When Count = 10000, that is, when writing 10000 lines of a file, the size of the file written is KB:
Fileoutputstream execution Duration: 188 seconds
Bufferedoutputstream execution time: 32 Hao seconds
Filewriter execution time: 16 seconds

 

3. When Count = 100000, that is, when writing 100000 rows of files, the size of the written files is 1856kb:
Fileoutputstream execution Duration: 1266 seconds
Bufferedoutputstream execution time: 125 seconds
Filewriter execution time: 93 seconds

 

4. When Count = 1000000, that is, when writing 1000000 lines of a file, the size of the file written is 18555kb:
Fileoutputstream execution Duration: 12063 seconds
Bufferedoutputstream execution time: 1484 seconds
Filewriter execution time: 969 seconds

From the above data, we can see that if bufferedoutputstream does not need to buffer the stream, the robustness of fileoutputstream writing files is very bad. When writing 1000000 rows of files, fileoutputstream is 11094 milliseconds slower than filewriter (11 seconds), and bufferedoutputstream is 515 milliseconds slower than filewriter.
Do not underestimate the time of these seconds. When the amount of data operated is large, this performance gap will be very large. When the general data migration tool exports tens of millions of records from the database to generate SQL script files, the performance may differ by more than 10 minutes.
The impact of Single-thread and multi-thread on performance will be written later.

Related Article

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.