"Simple Java" writes data in a file by line

Source: Internet
Author: User

The following code is a Java applet used to write data to a file. Each time you run the program, a new file is created, and the old file is deleted, which is different from appending to the file.

 public  static  void  writeFile1 ()  IOException {F        Ile fout  = new  File ("OUT.txt"  = new          FileOutputStream (Fout); BufferedWriter bw  = new  bufferedwriter (        New   OutputStreamWriter (FOS));  for  (int  i = 0; i <; I++ "Something" );        Bw.newline ();    } bw.close (); }

This example uses FileOutputStream, in addition to the class you can also use FileWriter or printwriter, for text manipulation, is usually sufficient;

Using FileWriter:

     Public Static void throws IOException {        new FileWriter ("OUT.txt");          for (int i = 0; i < ten; i++) {            fw.write ("Something");        }        Fw.close ();    }

Using PrintWriter:

     Public Static void throws IOException {        new printwriter (new FileWriter ("OUT.txt"));          for (int i = 0; i < ten; i++) {            pw.write ("Something");        }        Pw.close ();    }

Using OutputStreamWriter:

     Public Static void throws IOException {        new File ("OUT.txt");         New FileOutputStream (fout);         New outputstreamwriter (FOS);          for (int i = 0; i < ten; i++) {            osw.write ("Something");        }        Osw.close ();    }

Instructions in the Java documentation:

For writing character data to a file, FileWriter is a convenient class that constructs methods that use the default character encoding and byte buffer size. If you want to specify these values yourself, you can first construct a outputstreamwriter on the FileOutputStream.

PrintWriter is a formatted representation of the printed object to the text output stream, which implements all the print methods in the PrintStream, which do not contain methods for writing raw bytes that the program should write with an encoded byte stream.

The main difference is that PrintWriter provides some additional methods for formatting output, such as println, printf. In addition, if any I/O exception occurs, FileWriter throws IOException, and the second printwriter does not throw IOException, instead it sets a Boolean flag that can be called by the CheckError () Method gets. PrintWriter automatically calls the Flush method each time the data is written, but for FileWriter, the caller needs to pay special attention to the Flush method's invocation.

Link: http://www.programcreek.com/2011/03/java-write-to-a-file-code-example/

"Simple Java" writes data in a file by line

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.