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> |
Download this Program
No comments:
Post a Comment