Home Java Example Java Io Java Write To File - Java Tutorial
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java Write To File - Java Tutorial

Advertisement
In the section of Java Tutorial you will learn how to write java program to write to a file.

For writing data to a file, the class FileWriter and BufferedWriter are used.

 FileWriter :

 FileWriter is a subclass of OutputStreamWriter class that is used to write text (as opposed to binary data) to a file. You create a FileWriter by specifying the file to be written to, or optionally, when the data should be appended to the end of an existing file instead of overwriting that file. The FileWriter class creates an internal FileOutputStream to write bytes to the specified file

BufferedWriter :

The BufferedWriter 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. 
The constructor of the
FileWriter class takes the file name which has to be buffered by the BufferedWriter stream. The write( ) method of BufferedWriter class is used to create the file into specified directory.

Following code  write data into new file: 

out.write(read_the_Buffered_file_name);

Following code creates the object of FileWriter and BufferedWriter

FileWriter fstream = new FileWriter(file_name);
BufferedWriter out = new BufferedWriter(fstream);

Lets see an another example that writes the text input by the user using the FileWriter and the BufferedWriter class.

import java.io.*;

public class FileWriter{

  public static void main(String[] argsthrows IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Please enter the file name to create : ");
  String file_name = in.readLine();
  File file = new File(file_name);
  boolean exist = file.createNewFile();
  if (!exist)
  {
  System.out.println("File already exists.");
  System.exit(0);
  }
  else
  {
  FileWriter fstream = new FileWriter(file_name);
  BufferedWriter out = new BufferedWriter(fstream);
  out.write(in.readLine());
  out.close();
  System.out.println("File created successfully.");
  }
  }
}

Output of the Program 

C:\nisha>javac CreateFile.java

C:\nisha>java CreateFile
Please enter the file name to create : nishu.txt
this is my name
File created successfully.

C:\nisha>

 Download the code

Advertisement

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 17, 2007

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Sharada Ananya
September 13, 2011
CREATE AND WRITING INTO XML FILE

import java.io.*; public class FileWriter{ public static void main(String[] args) throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Please enter the file name to create : "); String file_name = in.readLine(); File file = new File(file_name); boolean exist = file.createNewFile(); if (!exist) { System.out.println("File already exists."); System.exit(0); } else { FileWriter fstream = new FileWriter(file_name); BufferedWriter out = new BufferedWriter(fstream); out.write(in.readLine()); out.close(); System.out.println("File created successfully."); } } } THIS CODE WORKS BUT ITS NOT WORKING FOR CREATING A XML FILES. ITS SHOWING ERROR LIKE Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later. -------------------------------------------------------------------------------- System does not support the specified encoding. Error processing resource PLEASE HELP ME OUT TO SOLVE THIS PROBLEM
Esaux
November 22, 2011
Question

How do i write to a file at the server-side data i have received from the client side using sockets in java programming language?
veeranagoud
December 28, 2011
all programs java,

java programs good...............
anilkumar
February 11, 2012
bestexample

very easy example this bufferwriter
dan
March 29, 2012
!exist is true if the file exists?

poor variable name choice.
petaro
August 24, 2012
quick question

where does this save the file to?
HONEY
November 19, 2012
easy way

as per my view to read from file program is much easiler . here you have added unnecessary coding .. it might be easy 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()); } } }
Savio
February 12, 2014
Bad variable selection

if (!exist) true -> it means file exists? Who writes like that. Hagti hain.
DMCA.com