4 ). Program to persist the data into a file using BufferedOutputStream
#1). Buffered classes are used to increase the performance of the application.
#2). When ever the Buffered Classes are used by default a small amount of memory will be created with the size of 512 bytes.
#3). The data need to be written into a file, first data will be stored in the buffer then the entire data will be dumped to a file.(present with in the buffer).
#4). For each FileInputStream class there will be associated BufferedInputStream class.
#5). These BufferedStream classes will take the other stream classes objects as an arguments.
read the file and save into array of bytes using getBytes( ) method.
and write the object into the bos object.
BufferedOutputStream working flow |
#3). The data need to be written into a file, first data will be stored in the buffer then the entire data will be dumped to a file.(present with in the buffer).
#4). For each FileInputStream class there will be associated BufferedInputStream class.
String str="hi this is muralidhar"; FileOutputStream fos=new FileOutputStream("hello.dat");
#5). These BufferedStream classes will take the other stream classes objects as an arguments.
BufferedOutputStream bos = new BufferedOutputStream(fos);
read the file and save into array of bytes using getBytes( ) method.
and write the object into the bos object.
package javabynataraj.iopack; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class BufferedOutput { public static void main(String[] args) { String str="hi this is muralidhar"; try{ FileOutputStream fos=new FileOutputStream("hello.dat"); BufferedOutputStream bos = new BufferedOutputStream(fos); byte by[] = str.getBytes(); bos.write(by); bos.close(); fos.close(); System.out.println("Done........!"); } catch(IOException ioe){ ioe.printStackTrace(); } } }