For fileoutputstream, bufferedoutputstream, and filewriter in the Java. Io package,
The test code and result are as follows:
Environment: machine IBM ThinkPad 61i,
Configuration: 2 GB dual-core CPU, 2 GB memory, XP SP2 operating system, Java api1.5.
- Import
Java. Io. bufferedoutputstream;
- Import
Java. Io. file;
- Import
Java. Io. fileoutputstream;
- Import
Java. Io. filewriter;
- Public
Class
Testtxtwriter {
Public
Testtxtwriter (){
- }
Public
Static
Void
Main (string [] ARGs ){
- Fileoutputstream out =
Null
;
- Fileoutputstream outstr =
Null
;
- Bufferedoutputstream buff =
Null
;
- Filewriter fw =
Null
;
Int
Count =
10000000
;
// Number of lines written to the file
Try
{
- Out =
New
Fileoutputstream (
New
File (
"D:/fileoutputstream.txt"
));
Long
Begin = system. currenttimemillis ();
For
(
Int
I =
0
; I <count; I ++ ){
- Out. Write (
"Test fileoutputstream, bufferedoutputstream, and filewriter to write/R/N"
. Getbytes ());
- }
- Out. Close ();
Long
End = system. currenttimemillis ();
- System. Out. println (
"Fileoutputstream execution time :"
+ (End-begin) +
"Haoxi"
);
- Outstr =
New
Fileoutputstream (
New
File (
"D:/bufferedoutputstream.txt"
));
- Buff =
New
Bufferedoutputstream (outstr );
Long
Begin0 = system. currenttimemillis ();
For
(
Int
I =
0
; I <count; I ++ ){
- Buff. Write (
"Test fileoutputstream, bufferedoutputstream, and filewriter to write/R/N"
. Getbytes ());
- }
- Buff. Flush ();
- Buff. Close ();
Long
End0 = system. currenttimemillis ();
- System. Out. println (
"Bufferedoutputstream execution time :"
+ (End0-begin0) +
"Haoxi"
);
- FW =
New
Filewriter (
"D:/filewriter.txt"
);
Long
Begin3 = system. currenttimemillis ();
For
(
Int
I =
0
; I <count; I ++ ){
- FW. Write (
"Test fileoutputstream, bufferedoutputstream, and filewriter to write/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 = 1 000, that is, when writing 1 000 lines of files, the size of the written files is 66.4kb:
Fileoutputstream execution time: 31 Hao seconds
Bufferedoutputstream execution time: 16 Hao seconds
Filewriter execution time: 0 seconds
2. When Count = 10 000, that is, when writing 10 000 lines of files, the size of the written files is 664kb:
Fileoutputstream execution time: 78 seconds
Bufferedoutputstream execution time: 32 Hao seconds
Filewriter execution time: 15 seconds
3. When Count = 100 000, that is, when writing 100 rows of files, the size of the written files is 6.7 MB:
Fileoutputstream execution Duration: 453 seconds
Bufferedoutputstream execution time: 172 seconds
Filewriter execution time: 156 seconds
4. When Count = 64.8 000, that is, when the number of lines written to the file is 000, the size of the file written is MB:
Fileoutputstream execution Duration: 4500 seconds
Bufferedoutputstream execution time: 2422 seconds
Filewriter execution time: 2500 seconds
5. When Count = 10 000 000, that is, when writing 10 000 lines of files, the size of the files written is 648 MB:
Fileoutputstream execution Duration: 52453 seconds
Bufferedoutputstream execution time: 25921 seconds
Filewriter execution time: 36094 seconds
Fileoutputstream execution Duration: 51547 seconds
Bufferedoutputstream execution time: 35203 seconds
Filewriter execution time: 31609 seconds
Fileoutputstream execution Duration: 50078 seconds
Bufferedoutputstream execution time: 33515 seconds
Filewriter execution time: 29516 seconds
By
The above data shows that if bufferedoutputstream and fileoutputstream do not need to buffer the stream, the file write robustness is very bad. When writing 10
Fileoutputstream is 10, 000-21, 000 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 operating on the database and generating hundreds of millions of records through Java export, it takes more than 10 minutes to wait, resulting in a high performance gap.