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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Write to a file  
 

As we have read about the BufferedOutputStream class that store the data in an internal buffer and lets you write characters to a stream. Lets see an example that writes the text "Hello Java" to the existed file.

 

Write to a file

                         

As we have read about  the BufferedOutputStream class that store the data in an internal buffer and lets you write characters to a stream. Lets see an example that writes the text "Hello Java" to the existed file.

The given example uses the BufferedOutputstream class that writes the bytes to the output stream. The output stream is a file "Filterfile.txt" in which the data is written in form of a byte. The getBytes( ) method of the String class returns the bytes from the string.

 

import java.io.*; 

class WriteFilter
{
    public static void main(String args[])
    {
    String str="Hello Java";
        try 
        {
      FileOutputStream fos = new FileOutputStream("Filterfile.txt");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
      
            // Now write to the buffered stream.
           bos.write(str.getBytes());
           bos.flush();
                System.out.print("the data has been written");                 
        
        catch (Exception e
        {
            System.err.println("Error writing to a file: " + e);
        }
    }
}

Output of the Program:

C:\nisha>javac WriteFilter.java

C:\nisha>java WriteFilter
the data has been written
C:\nisha>

Download this Program

                         

» View all related tutorials
Related Tags: c orm form time script object io objects help method sed system ip collection this opera create show for work

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

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

So this creates a Filterfile.txt and contains "Hello java". Instead of containing "Hello Java" how would we make it so the new .txt file contains contents from another file?

Thanks

Posted by backslash on Tuesday, 04.7.09 @ 12:05pm | #86594

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
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.