Recently, operations on large files are required in the project, and Java I/O tests are conducted.
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.
1. Import java. Io. bufferedoutputstream;
2. Import java. Io. file;
3. Import java. Io. fileoutputstream;
4. Import java. Io. filewriter;
5.6.public class testtxtwriter {
7.8. Public testtxtwriter (){
9 .}
10.11. Public static void main (string [] ARGs ){
12. fileoutputstream out = NULL;
13. fileoutputstream outstr = NULL;
14. bufferedoutputstream buff = NULL;
15. filewriter fw = NULL;
16. Int COUNT = 10000000; // number of lines written to the file 17. Try {
18. Out = new fileoutputstream (new file ("D:/fileoutputstream.txt "));
19. Long begin = system. currenttimemillis ();
20. For (INT I = 0; I <count; I ++ ){
21. Out. Write ("test fileoutputstream, bufferedoutputstream, and filewriter. Write to files \ r \ n". getbytes ());
22 .}
23. Out. Close ();
24. Long end = system. currenttimemillis ();
25. system. Out. println ("fileoutputstream execution time:" + (end-begin) + "haoxi ");
26.27. outstr = new fileoutputstream (new file ("D:/bufferedoutputstream.txt "));
28. Buff = new bufferedoutputstream (outstr );
29. Long begin0 = system. currenttimemillis ();
30. For (INT I = 0; I <count; I ++ ){
31. Buff. Write ("test fileoutputstream, bufferedoutputstream, and filewriter. Write to files \ r \ n". getbytes ());
32 .}
33. Buff. Flush ();
34. Buff. Close ();
35. Long end0 = system. currenttimemillis ();
36. system. Out. println ("bufferedoutputstream execution time:" + (end0-begin0) + "haoxi ");
37.38. fw = new filewriter ("D:/filewriter.txt ");
39. Long begin3 = system. currenttimemillis ();
40. For (INT I = 0; I <count; I ++ ){
41. FW. Write ("test fileoutputstream, bufferedoutputstream, and filewriter, write to files \ r \ n ");
42 .}
43. FW. Close ();
44. Long end3 = system. currenttimemillis ();
45. system. Out. println ("filewriter execution time:" + (end3-begin3) + "haoxi ");
46.47.} catch (exception e ){
48. E. printstacktrace ();
49.} finally {
50. Try {
51. FW. Close ();
52. Buff. Close ();
53. outstr. Close ();
54. Out. Close ();
55.} catch (exception e ){
56. E. printstacktrace ();
57 .}
58 .}
59 .}
60 .}
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
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. Fileoutputstream is 10 000-21 000 milliseconds slower than filewriter when writing 10 000 lines of files (10-21 seconds ).
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.
Article Source: feino Network (www.diybl.com): http://www.diybl.com/course/3_program/java/javajs/20090405/164173.html