Fri Oct 08 2021
BufferedByteStream
Java Programming1851 views
File Name: bufferedbytestream-file.java
/* Buffered byte stream */
import java.io.*;
class bufferedfile {
public static void main(String args[ ]) throws IOException {
/* Declare and create input file */
FileOutputStream wf = new FileOutputStream("bufferdfile.txt");
BufferedOutputStream wb = new BufferedOutputStream(wf);
byte text[] = {'B', 'u', 'f', 'f', 'e', 'r', 'd', ' ', 'S', 't', 'r', 'e', 'a', 'm', ' ', 'F', 'i', 'l', 'e'};
wb.write(text);
System.out.println("File created!");
wb.close();
wf.close();
/* Read till end of the file */
FileInputStream rf = new FileInputStream("bufferdfile.txt");
BufferedInputStream rb = new BufferedInputStream(rf);
System.out.println("Reading File -");
int data = 0;
byte[] contents = new byte[1024];
while ((data = rb.read(contents)) != -1) {
/* Convert buffered stream to string */
String word = new String(contents, 0, data);
System.out.println(word);
}
rb.close();
rf.close();
}
}
/* Output */
File Created!
Reading File -
Bufferd Stream File
Reference:
FileOutputStream, FileInputStream, BufferedOutputStream and BufferedInputStream
Author:Geekboots