Read File BufferedInputStream

Read File BufferedInputStream

Below java program can be used to Read File using BufferedInputStream:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ReadFileByBufferedInputStream {

	public static void main(String[] args) {

		File file = new File("C://JavaHorn//Test.txt");
		BufferedInputStream bin = null;

		try {
			// create FileInputStream object
			FileInputStream fin = new FileInputStream(file);

			// create object of BufferedInputStream
			bin = new BufferedInputStream(fin);

			// read file
			while (bin.available() > 0) {

				System.out.print((char) bin.read());
			}

		} catch (FileNotFoundException e) {
			System.out.println("File not found" + e);
		} catch (IOException ioe) {
			System.out.println("Exception while reading the file " + ioe);
		} finally {
			try {
				if (bin != null)
					bin.close();

			} catch (IOException ioe) {
				System.out.println("Error while closing the stream : " + ioe);
			}

		}
	}

}

Leave a Reply

Your email address will not be published. Required fields are marked *