Home Java Beginners 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.

Java Write To File - Java Tutorial

Learn how to write to a file from Java program. This tutorial teaches you how you can use the OutputStreamWriter on a FileOutputStream classes to write data to a file from Java program.

     

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.

Advertisement

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

If you execute the above code program will write "Hello Java" into out.txt file. While creating the object of FileWriter class we have passed the name of the file e.g. "out.txt" as constructor argument.

The BufferedWriter class takes FileWriter object as parameter. The write() method of the BufferedWriter class actually writes the data into the text file.

Advertisements

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: June 5, 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
kay mwando
March 30, 2011
java

i would like to know how to produce a suitable file of text for analysis
ramanna
April 19, 2011
java

how to save the java program, and compile see the output?
mirusha
April 19, 2011
java

hi, i am stuck on my course work about a library system in java. i just create some GUI buttons for my library system. but i dont know how to link them. pls help me how can i link them
Ahmed`
April 25, 2011
thanks

thanks so much for the code
Alex
June 13, 2011
Error handling

I agree to the previous comment about the catching a nonspecified error being dangerous. Also, you should have the "out.close()" in a finally block, since if the application crashes before it reaches this line, the writer will remain open. ... catch (IOException e){ // Handle exception } finally { out.close(); }
global
July 1, 2011
java

hi who can help me??? i wanna a program by java language and it is (simple form of excel microsoft)
Divs
July 12, 2011
code is not working

Get following erro. Exception in thread "main" java.lang.UnsupportedClassVersionError: FileWrite (Un supported major.minor version 50.0)
11
September 6, 2011
sdfsdf

not working in eclipse.
aayushi
March 2, 2013
File Splitter

Sir how to break a file in different smaller Files?
shanto Rahman
October 8, 2011
this description

Description is not clear........ please help...... i am a newly learned in java.....so plz help
James
January 27, 2013
Will not write

I'm having no errors but everytime i try to do this it will not work, i've even tried copying and pasting the exact code, and it will not write anything into the file, it comes up with no errors but when i run the program and then open the text file created it is just blank, any help would really be appreciated thanks :)
Deepak
January 28, 2013
File write example working fine

Hi, I have tested this example and it is working fine. Thanks
jagath
November 29, 2011
aaaaaa

what is try catch
Luiz
December 9, 2011
error

plz when i try to do that with ints, the txt file dont recognize them... HELP plz!
sangeetha
January 11, 2012
regarding file writing

HI, can anyone explain me how to write the template file in external and then calling them in java. Please help. Thanks in advance!!!
kaiz
February 8, 2012
about this

hi, can you tell me where the file out.txt will be created?
lhogeshvaree
February 23, 2012
java

i wat a java code to read a file line by line
bam
March 1, 2012
file writer

cool dude
nikhil sharma
April 1, 2012
doesnot worked for me

this code is not working, the file is created but no data is inserted in the file please update
nikhil sharma
April 1, 2012
ok

i get it ,it doesnot write data in the file until u close the connection.
Terra
April 14, 2012
Java I/O

How would I input and out put files to an external file using array data including date?
aisha abdallah
May 5, 2012
thanks

this site is very very helpful thank you :)
Sinned
May 19, 2012
Thank you

This was the first result for the search "how to write to a file in java" and it has helped me and worked great. Simplicity is great, thanks for writing this up.
Notch
May 26, 2012
App

That doesn't work
ali gaber
June 22, 2012
student

realy thank you alot..your site is very useful site thanks again :>
Lucian
July 10, 2012
Great!

Thanks, simple and straight forward!
Rahul Raj
July 20, 2012
java tutorials

your site is so helpfull. thank you for creating like this.
md.rahid islam
July 30, 2012
how to making ajava programimg file

how to making ajava programimg lpz give me not
md.rahid islam
July 30, 2012
ajava programimg

how to making ajava programimg file give me the letter
Z
August 9, 2012
Tags?

Why so many?
Maarten
December 7, 2012
Java Write To File - Java Tutorial

Why not put the out.Close() in a finally?
Starkiller2
November 28, 2012
Thank you!

I was having problems with IO and after scrutinizing this code I realized that try blocks should contain ALL IO stuff. Then I was having variable problems because I used multiple try-catch blocks, and the variables did not carry over from one block to another.
Seetha
July 12, 2013
how to read and write excel file in java

hi
kaliso kalawe
January 21, 2014
java file handling

need help for java subject
Fizza
May 25, 2014
how to append

this code works fine for me but it overwrites everything that is written in the file befor, how can i appned at the end of file ?
DMCA.com