Conversion: Comparison of fileoutputstream, bufferedoutputstream, and filewriter for writing text files using Java APIs

Source: Internet
Author: User

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. Public
     
    Class
    Testtxtwriter {

  6. Public
    Testtxtwriter (){
  7. }

  8. Public
     
    Static
     
    Void
    Main (string [] ARGs ){
  9. Fileoutputstream out =
    Null
    ;
  10. Fileoutputstream outstr =
    Null
    ;
  11. Bufferedoutputstream buff =
    Null
    ;
  12. Filewriter fw =
    Null
    ;

  13. Int
    Count =
    10000000
    ;
    // Number of lines written to the file

  14. Try
    {
  15. Out =
    New
    Fileoutputstream (
    New
    File (
    "D:/fileoutputstream.txt"
    ));

  16. Long
    Begin = system. currenttimemillis ();

  17. For
    (
    Int
    I =
    0
    ; I <count; I ++ ){
  18. Out. Write (
    "Test fileoutputstream, bufferedoutputstream, and filewriter to write/R/N"
    . Getbytes ());
  19. }
  20. Out. Close ();

  21. Long
    End = system. currenttimemillis ();
  22. System. Out. println (
    "Fileoutputstream execution time :"
    + (End-begin) +
    "Haoxi"
    );
  23. Outstr =
    New
    Fileoutputstream (
    New
    File (
    "D:/bufferedoutputstream.txt"
    ));
  24. Buff =
    New
    Bufferedoutputstream (outstr );

  25. Long
    Begin0 = system. currenttimemillis ();

  26. For
    (
    Int
    I =
    0
    ; I <count; I ++ ){
  27. Buff. Write (
    "Test fileoutputstream, bufferedoutputstream, and filewriter to write/R/N"
    . Getbytes ());
  28. }
  29. Buff. Flush ();
  30. Buff. Close ();

  31. Long
    End0 = system. currenttimemillis ();
  32. System. Out. println (
    "Bufferedoutputstream execution time :"
    + (End0-begin0) +
    "Haoxi"
    );
  33. FW =
    New
    Filewriter (
    "D:/filewriter.txt"
    );

  34. Long
    Begin3 = system. currenttimemillis ();

  35. For
    (
    Int
    I =
    0
    ; I <count; I ++ ){
  36. FW. Write (
    "Test fileoutputstream, bufferedoutputstream, and filewriter to write/R/N"
    );
  37. }
  38. FW. Close ();

  39. Long
    End3 = system. currenttimemillis ();
  40. System. Out. println (
    "Filewriter execution time :"
    + (End3-begin3) +
    "Haoxi"
    );
  41. }
    Catch
    (Exception e ){
  42. 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.

    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.