Write Text into File

In this section, you will learn how to write text into a file.

Write Text into File


In this example we are writing text into file.In this example we are initialize string to write into file. We are creating file in which we are writing string by use of write() method.

We need  java.io.*  package import first. The create   a .txt file with name "write.txt". We are using FileWriter class to read file for modification. BufferedWriter class is used for buffering the file which will store into an object of  Writer class .Then we are using write() method to write text into file. At last close output file using close() method.

The code of the program is given below:

import java.io.*;

public class WriteTextFileExample{
  public static void main(String[] args)throws IOException{
  Writer output = null;
  String text = "Rajesh Kumar";
  File file = new File("write.txt");
  output = new BufferedWriter(new FileWriter(file));
  System.out.println("Your file has been written");  

The output of the program is given below:

C:\convert\rajesh\completed>javac WriteTextFileExample.java
C:\convert\rajesh\completed>java WriteTextFileExample
Your file has been written

Download this example.

Share on Google+Share on Google+

Write Text into File

Posted on: June 29, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Write Text into File   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Arvind K
December 17, 2011
Regarding post.

The post was useful...
February 19, 2012

Thanks for this post! it's easy to understand!
February 27, 2012

import java.io.*; import java.lang.*; import java.util.*; public class Cf { private Formatter x; public void openFile() { try { x = new Formatter("C:/Users/travikanth/Workspaces/MyEclipse 8.x/practice/src/po.txt"); } catch(Exception e) { System.out.println("error"+ e.toString()); } } public void addRecords() { x.format("%s,%s,%s", "2","ravi","23"); } public void closeFile() { x.close(); } public static void main(String[] args) { Cf a = new Cf(); a.openFile(); a.addRecords(); a.closeFile(); } }
April 3, 2012
I get an error

When i try to complie i get the error below Could some help "Error: The public type WriteTextFileExample must be defined in its own file"
July 23, 2012

Hello there, This code does not really work for me. Here's how I tetesd it. My test.txt file is saved with UTF-8 encoding and contains this line: w e9te9 jedn? stron? f4pe8e7 dbg fcte0 My test program below first reads the file in BufferedReader and then writes in Writer. package test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer;public class Test_temp { public Test_temp() { String sPath = E:/workspace/project/src/test/test.txt ; if (sPath != null && !sPath.trim().equals( )) { try { Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sPath + .new ), UTF8 )); BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(sPath), UTF8 )); String s = null; while ((s = in.readLine()) != null) { String UTF8Str = new String(s.getBytes(), UTF8 ); System.out.println( [" + UTF8Str + "] ); out.append(UTF8Str).append( \r\n ); } System.out.println( Reading Process Completly Successfully. ); in.close(); out.flush(); out.close(); } catch (UnsupportedEncodingException ue) { System.out.println( Not supported : + ue.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } } } public static void main(String[] args) { new Test_temp(); } } The new generated file (test.txt.new) is also encoded with UTF-8 but characters are corrupted: ?w ?t? jedn? stron? ?p?? ?g ? Could you please tell me what do I do wrong? Thanks
md.rahid islam
July 30, 2012

how to making ajava programimg file
September 14, 2012

how to write java program in ile
September 19, 2012

can you give some more complex examples
October 3, 2012

where will the file be saved?