Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  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. 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

                         

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

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

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

Latest Searches:
create a directory and
http servlet
container managed pers
program in java to ins
my Sql server through
transact sql
displaytag
ignore
struts hibernates myec
utility of collections
how to load a frame us
myeclipse
Two Dimensional Array
how 2 comment a java l
Persistent Supplier ob
setvisible tab
struts html tag exampl
csv file format
Photoshop Digital Art
jfreechart
how to declare an arra
CMP Entity bean
user id js
retrieving file from
JCalendar
structs2 action
frame
rtsp
prime numbers between
3d max
mobile testing
dojo Spring
applet in dojotype
Custom Rendering of JL
Photoshop Effects Lake
previous
JSF panelGrid render
Filtering Selection fr
Read the Key-Value of
c:out in var of c:fore
draw triangle
contact
ListIterator
jdbctemplate.execute
code for i create a ta
clear values from a fo
selecting multiple fil
add picture to a swing
PHP Creating a Mail Fo
builb.xml
String to array in jav
covert uppercase to lo
how to use tab control
parse a int to string
save image to file
jboss portal
form bean update
Get Array List in a JS
jQuery To Hide the Div
Websphere portal 6.0
show calender days in
Photoshop Web Graphics
Java Count Vowels
compare two arraylist
jsp pdf
JSP Cookies Example
Display Data from Data
java registration form
Button method
File Separator
Nested loops for JAVA
PHP Database Related C
rules to use java coll
Combattons la programm
inserting select box m
driving
file upload in jsp
message box display in
File Upload
pst.setInt()
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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 | 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.