Read File into String Using BufferedInputStream in Java
Description
The following code shows how to read File into String Using BufferedInputStream.
Example
/* w w w . j a v a2s . co m*/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("main.java");
FileInputStream fin = new FileInputStream(file);
BufferedInputStream bin = new BufferedInputStream(fin);
byte[] contents = new byte[1024];
int bytesRead = 0;
String strFileContents;
while ((bytesRead = bin.read(contents)) != -1) {
strFileContents = new String(contents, 0, bytesRead);
System.out.print(strFileContents);
}
bin.close();
}
}
The code above generates the following result.