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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Working with PrintStream 
 

The PrintStream class is obtained from the FilterOutputstream class that implements a number of methods for displaying textual representations of Java primitive data types.

 

Working with PrintStream

                         

The PrintStream class is obtained from the FilterOutputstream class that implements a number of methods for displaying textual representations of Java primitive data types. It adds the functionality to another output streams that has the ability to print various data values conveniently. Unlike other output streams, a PrintStream never throws an IOException and the data is flushed to a file automatically i.e. the flush method is automatically invoked after a byte array is written,

The constructor of the PrintStream class is written as:

PrintStream (java.io.OutputStream out);   //create a new print stream

The print( ) and Println( ) methods of this class give the same functionality as the method of standard output stream and follow the representations with newline.

The given example demonstrate the writing operation to a file using PrintStream class.

import java.io.*;

class PrintStreamDemo {  
        
public static void main(String args[]){   
               
FileOutputStream out; 
                PrintStream ps; // declare a print stream object
                try {
                 // Create a new file output stream
                out = new FileOutputStream("myfile.txt");

                        // Connect print stream to the output stream
                        ps = new PrintStream(out);
    
                        ps.println ("This data is written to a file:");
            System.err.println ("Write successfully");
                        ps.close();
                }
                catch (Exception e){
                        System.err.println ("Error in writing to file");
                }
        
}
}


Output of the Program:

C:\nisha>javac PrintStreamDemo.java

C:\nisha>java PrintStreamDemo
Write successfully

C:\nisha>

This program firstly create an object "ps" of the PrintStream class the specified data is written through that object using the println( ) method.

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