Here we are discussing about reading the file using BufferedInputStream.
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();
}
}
}
}