方式一(需要手动关闭流)
import java.io.*;
public class Demo11 {
public static void main(String[] args) throws IOException {
BufferedInputStream buff = null;
try {
buff = new BufferedInputStream(new FileInputStream("c.txt"));
byte[] arr = new byte[8192];
int count;
while ((count = buff.read(arr) )!= -1 ) {
System.out.println(new String(arr));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(buff != null)
buff.close();
}
}
}
方式二(无需手动调用关闭流的close方法,自动调用)
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class Demo12 {
public static void main(String[] args) throws Exception{
try(BufferedInputStream buff = new BufferedInputStream(new FileInputStream("c.txt"))){
byte[] arr = new byte[8192];
int count;
while ((count = buff.read(arr)) != -1) {
System.out.println(new String(arr));
}
}
}
}