Fri Oct 08 2021

BufferedByteStream

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

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.