Home Java Beginners Write Text into File
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Write Text into File

Advertisement
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));
  output.write(text);
  output.close();
  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.

Advertisements

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: June 29, 2007

Related Tutorials

Ask Questions?    Discuss: Write Text into File   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
acl
May 24, 2011
fileWriter location

WHERE would the txt file be located? How would you set it if you wanted to create a txt file, write to it, and place it in like...your instructors computer for evaluation? : )
Misha
May 28, 2011
Nice!

How I able to save the file in the path that I choose?
jeryl
September 20, 2011
hi :)

this really helped me with my project... the right source :) thanks
jeryl
September 20, 2011
hi :)

this really helped me with my project... the right source :) thanks
Arvind K
December 17, 2011
Regarding post.

The post was useful...
Apoorva
February 19, 2012
Works!

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

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(); } }
kiboigo
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"
Antonio
July 23, 2012
gAfCGlnzmsght

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
programimg

how to making ajava programimg file
venkataramana
September 14, 2012
as400

how to write java program in ile
Kym
September 19, 2012
JAVA

can you give some more complex examples
yhen
October 3, 2012
question

where will the file be saved?
DMCA.com