How do I write a file in Java on a row-by-line basis?

Source: Internet
Author: User

Below is the Java code that writes something to a file.

each time after execution, a new file is created, and the previous one will be replaced by the new file. This is different from appending content to the file.

public static void WriteFile1 () throws IOException {file Fout = new File ("OUT.txt"); FileOutputStream fos = new FileOutputStream (fout); BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (FOS)); for (int i = 0; i < i++) {Bw.write ("something"); Bw.newline ();} bw.close ();}

This example uses FileOutputStream, and you can also use FileWriter or PrintWriter. The assumption is that the operation against a text file is fully sufficient.

Using FileWriter:

public static void WriteFile2 () throws IOException {FileWriter fw = new FileWriter ("OUT.txt"), for (int i = 0; i < 10; i++) {fw.write ("something");} fw.close ();}
using PrintWriter:
public static void WriteFile3 () throws IOException {PrintWriter pw = new PrintWriter (New FileWriter ("OUT.txt")); for (int i = 0; I < 10; i++) {pw.write ("something");} pw.close ();}
using OutputStreamWriter:
public static void WriteFile4 () throws IOException {file Fout = new File ("OUT.txt"); FileOutputStream fos = new FileOutputStream (fout); OutputStreamWriter OSW = new OutputStreamWriter (FOS); for (int i = 0; i < i++) {Osw.write ("something");} osw.close ();}
excerpt from the Java Documentation:

filewriter is a convenience class for writing character files.  The Constructors of this class assume the default character encoding and the default Byte-buffer size is ACCEPTABLE.&NB SP; To Specify these Values yourself, construct an outputstreamwriter on a fileoutputstream.

FileWriter is a very handy class for writing character files. the method of constructing this class is acceptable if the default character encoding and the default byte buffer are accepted.

If you want to specify the length of the encoding and byte buffers, you need to construct outputstreamwriter.


printwriter prints formatted representations of objects to a text-output stream.  this class implements all of the print methods found in printstream.  it does not contain methods for writing raw Bytes, for which a program should use unencoded byte streams.

PrintWriter Prints the representation of the formatted object to a text output stream. This class implements all of the printing methods in PrintStream.

It does not include a byte stream that is used to write raw bytes, because a program should use an encoding.

The main difference is that PrintWriter provides some additional ways to format it. Like println and printf.

In addition, in the event of any I/O failure filewriter will throw IOException. The PrintWriter method does not throw IOException exceptions, but instead they set a Boolean flag value that can be used to detect errors (CheckError ()).

PrintWriter automatically calls flush after each byte of the data is written. in FileWriter, the caller must take a manual call to flush.

What is the original: How to Write a File line by line in Java?



How do I write a file in Java on a row-by-line basis?

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.