Home Java Beginners Java Write To File - Java Tutorial



Java Write To File - Java Tutorial
Posted on: June 5, 2007 at 12:00 AM
In the section of Java Tutorial you will learn how to write java program to write to a file.

Java Write To File - Java Tutorial

     

In the section of Java Tutorial you will learn how to write java program to write to a file. We will use the class FileWriter and BufferedWriter to write to a file.

Class FileWriter
The FileWriter is a class used for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.

BufferedWriter

The BufferWriter class is used to write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

Here is the code of java program to write text to a file:

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());
  }
  }
}

Download the code

Related Tags for Java Write To File - Java Tutorial:
cfileclassfilestableencodingconstructorstructsizesedoutputstreamconstchardefaultbuffervaluesumbytethiscodingwritecharactertabifforcteconstructorselfwritingvaluestoselfcieilitstreamwriterputuseulpeoutputstreamcefaultinasmouttrclesspecmeaccepttorwriterssuspctoratracishalffileoutputstreameaandaractcodccconsstrtstreamvaufzssrithstabablaluhatctofefileoutputefaultoutputsptandono


Ask Questions?    Discuss: Java Write To File - Java Tutorial   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.