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
 
 
Search All Tutorials
  

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

Java Write To File - Java Tutorial

                         

Introduction

In the section, you will learn how to write data to a file. As we have discussed, the FileOutputStream class is used to write data to a file. 

Lets see an example that writes the data to a file converting into the bytes.

This program first check the existence of the specified file. If the file exist, the data is written to the file through the object 
of the FileOutputStream class. 

 

 

import java.io.*;

public class WriteFile{

    public static void main(String[] argsthrows IOException{

      File f=new File("textfile1.txt");
      FileOutputStream fop=new FileOutputStream(f);

      if(f.exists()){
      String str="This data is written through the program";
          fop.write(str.getBytes());

          fop.flush();
          fop.close();
          System.out.println("The data has been written");
          }

          else
            System.out.println("This file is not exist");
    }
  }


Output of the Program
 

C:\nisha>javac WriteFile.java

C:\nisha>java WriteFile
The data has been written

C:\nisha>

Download this Program

The another way for writing data to a file, the class FileWriter and BufferedWriter are used.

 FileWriter :

 FileWriter is a subclass of OutputStreamWriter class that is used to write text (as opposed to binary data) to a file. You create a FileWriter by specifying the file to be written to, or optionally, when the data should be appended to the end of an existing file instead of overwriting that file. The FileWriter class creates an internal FileOutputStream to write bytes to the specified file

BufferedWriter :

The BufferedWriter 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. 
The constructor of the
FileWriter class takes the file name which has to be buffered by the BufferedWriter stream. The write( ) method of BufferedWriter class is used to create the file into specified directory.

Following code  write data into new file: 

out.write(read_the_Buffered_file_name);

Following code creates the object of FileWriter and BufferedWriter

FileWriter fstream = new FileWriter(file_name);
BufferedWriter out = new BufferedWriter(fstream);

Lets see an another example that writes the text input by the user using the FileWriter and the BufferedWriter class.

import java.io.*;

public class FileWriter{

  public static void main(String[] argsthrows IOException{
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please enter the file name to create : ");
    String file_name = in.readLine();
    File file = new File(file_name);
    boolean exist = file.createNewFile();
    if (!exist)
    {
      System.out.println("File already exists.");
      System.exit(0);
    }
    else
    {
      FileWriter fstream = new FileWriter(file_name);
      BufferedWriter out = new BufferedWriter(fstream);
      out.write(in.readLine());
      out.close();
      System.out.println("File created successfully.");
    }
  }
}

Output of the Program 

C:\nisha>javac CreateFile.java

C:\nisha>java CreateFile
Please enter the file name to create : nishu.txt
this is my name
File created successfully.

C:\nisha>

 Download the code

                         

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

Current Comments

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

what is the use of using "getbytes()"while writing to a file?

Posted by ankitamishra on Monday, 05.12.08 @ 11:39am | #59429

I'm new to html and java programing and I've been trying off and on for 3 days now trying to figure out how to read and write data to a file using html or java. Could you PLEASE send me a html page example with or without java that will read/write data to a file?

Posted by reid on Saturday, 03.8.08 @ 12:30pm | #51906

Try this

if(in.ready()) {
message = in.readLine();
}

Posted by EFG on Thursday, 02.28.08 @ 21:21pm | #50603

Hi,
I want to know how to append data to an already existing file using BufferedReader.
I did it but null value is appending.
Can anyone tell me the solution.
Thanks & Regards,
Keerthi Bodepudi.

Posted by keerthi on Monday, 02.25.08 @ 09:23am | #49917

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

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

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

Copyright © 2007. All rights reserved.