Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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.

 

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

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

8 comments so far (
post your own) View All Comments Latest 10 Comments:

Can anybody tell me how to append in an existing excel sheet using java. I am able to write in excel but not able to append it.

Posted by Mansiha on Friday, 04.10.09 @ 10:27am | #86697

How do write a newline to file

Posted by Robert on Sunday, 12.14.08 @ 01:10am | #82731

This is not good code. You should never write a "catch (Exception e)" block because it is dangerous. It allows exceptions you did not expect to be caught and ignored. This damages the safety of your system.

Also, you are ignoring the exception once it's caught. It would be much better if you declared your method to throw IOException and got rid of the try/catch.

Posted by Bob on Tuesday, 09.30.08 @ 20:05pm | #80805

can you help how to open the result text file once the script is done ....

Posted by HariKishore on Tuesday, 10.2.07 @ 05:14am | #30897

Can u provide Complete detail course and with examples so we can understand easyly..
thank u

Posted by santosh on Wednesday, 04.4.07 @ 14:17pm | #13322

hi can someome please help me???

i want to read out a com port (gps)
and write the data it recieves to a .txt file
can any please help me?????

Posted by dap on Wednesday, 03.21.07 @ 14:28pm | #12346

i modified it so it accepts some text from the keyboard



package task1_11;
import java.io.*;
public class Task1_11 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
BufferedReader bf = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Save to a file(write some text) : ");
String readin=bf.readLine();

// Create file
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(readin);
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}

}

}

Posted by kasia on Thursday, 03.8.07 @ 19:12pm | #11169

This is helped me a lot.
But try to modify this to accept data from keyboard and write that data to a new file, that is a dynamic program

Posted by Venkatesh on Wednesday, 02.28.07 @ 14:15pm | #9943

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.