Three ways to write files in Java comparison

Source: Internet
Author: User

1.FileOutputStream mode

2.BufferedOutputStream mode

3.FileWriter mode

After many tests, found that the fileoutputstream will be slow, when the file is small, the relationship is not big, but when the file is large, the time spent will be very different

Package Filetest;import java.io.bufferedoutputstream;import java.io.file;import java.io.FileOutputStream; Import Java.io.filewriter;import java.io.IOException;/** * Three ways to write files **/ Public classOutputFile1 { Public Static voidMain (string[] args) {FileOutputStream out=NULL; FileOutputStream outstr=NULL; Bufferedoutputstream Buff=NULL; FileWriter FW=NULL; intCount = +; Try {             out=NewFileOutputStream (NewFile ("D:\\testfile\\test.txt")); LongBegin =System.currenttimemillis ();  for(intI=0; i<count; i++){                 out. Write ("test file operation \ r \ n". GetBytes ()); }             out. Close (); LongEnd =System.currenttimemillis (); System. out. println ("FileOutputStream method Time-consuming"+ (end-begin)); Outstr=NewFileOutputStream (NewFile ("D:\\testfile\\test1.txt")); Buff=NewBufferedoutputstream (OUTSTR); LongBegin0 =System.currenttimemillis ();  for(intI=0; i<count; i++) {Buff.write ("test file operation \ r \ n". GetBytes ());            } buff.flush ();            Buff.close (); LongEnd0 =System.currenttimemillis (); System. out. println ("Bufferedoutputstream method Time:"+ (end0-begin0)); FW=NewFileWriter (NewFile ("D:\\testfile\\test2.txt")); LongBegin1 =System.currenttimemillis ();  for(intI=0; i<count; i++) {Fw.write ("test file operation \ r \ n");            } fw.close (); LongEnd1 =System.currenttimemillis (); System. out. println ("FileWriter method Time:"+ (end1-begin1)); } Catch(Exception e) {//Todo:handle Exception}finally {            Try{fw.close ();                Buff.close ();                Outstr.close ();  out. Close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                }    }}

Operation Result:

FileOutputStream method takes 6
Bufferedoutputstream Mode time: 5
FileWriter Mode time: 4

Three ways to write files in Java comparison

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.