How to read a File using BufferedInputStream

Here we are discussing about reading the file using BufferedInputStream.

To learn to read File using BufferedReader  Click Here.

To learn to read File using Scanner  Click Here.

package JavaIOExample;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

/*
 * Here we will learn to read the file using BufferedInputStream. 
 */
public class ReadTheFileUsingBufferedInputStream {
	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Enter the File path");
		String filePath = scanner.next();

		BufferedInputStream bis = null;
		DataInputStream dis = null;
		FileInputStream fis = null;

		/*
		 * Creates a new File instance by converting the given pathname string
		 * into an abstract pathname. 
		 */
		File file = new File(filePath);

		try {
			fis = new FileInputStream(file);

			bis = new BufferedInputStream(fis);
			dis = new DataInputStream(bis);

			while (dis.available() != 0) {
				/*
				 * Read the next line of text from this input stream.
				 * Deprecated
				 */
				System.out.println(dis.readLine());
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				bis.close();
				dis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

}

 

By | 2017-07-24T12:29:00+00:00 October 20th, 2012|Code Base|0 Comments

About the Author:

Leave A Comment