Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javatutorial How to write a file in Java?

Related Tutorials


 
 

Share on Google+Share on Google+

How to write a file in Java?

Advertisement
How to write a file in Java? To write a file in Java use the class FileWriter and BufferedWriter. The FileWriter class is used to write data into output stream. However, one can specify these values by using OutputStreamWriter on a FileOutputStream. There are various constructors of FileWriter class that allows data writing in output stream in various ways.

How to write a file in Java? To write a file in Java use the class FileWriter and BufferedWriter.

Class FileWriter:

The FileWriter class is used to write data into output stream.

It is used for writing character files. By default, constructors of this class assume that character encoding and byte-buffer size are acceptable.

However, one can specify these values by using OutputStreamWriter on a FileOutputStream.

There are various constructors of FileWriter class that allows data writing in output stream in various ways.

Some of these constructors are as follows:

FileWriter(File file):

This constructors creates a FileWriter object with a given file object. Using this constructor data is written into a given file. If a value already exists in this file then the existing value will be overwritten by new value.

FileWriter(File file, boolean append):

This constructor creates a FileWriter object with a given file object and specify whether the value should be appended for the existing value or not depending on the value of append, which can be True or False.

FileWriter(String fileName):

This constructor creates a FileWriter object with a given  fileName.

FileWriter(String fileName, boolean append):

This constructor creates a FileWriter object with a given filename and specify whether the value should be appended for the existing value or not depending on the value of append, which can be True or False.

BufferedWriter:

The BufferWriter class is used to write data from buffer to character-output stream. Data can be single characters, arrays and strings.

Example of how to write text to a file in java:

import java.io.*;
class FileWrite 
{
 public static void main(String args[])
  {
  try{
  // Create file 
  FileWriter fstream = new FileWriter("out.txt");
  BufferedWriter out = new BufferedWriter(fstream);
  out.write("Hello Java");
  //Close the output stream
  out.close();
  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

The above example will make a text file by the name "out.txt" and will write "Hello Java" in it.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 20, 2013

Related Tutorials

Discuss: How to write a file in Java?  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com