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>java WriteFilter
the data has been written
C:\nisha>
No comments:
Post a Comment