字节输入流 使用for循环和while循环来得到文件中的数据
while循环方法:
public static void getWhile(FileInputStream fileInputStream) throws IOException {
byte[] bytes = new byte[1024];//设定缓冲区大小
int len = 0;
while ((len = fileInputStream.read(bytes)) != -1) {//判断fileInputStream.read(bytes)是否为-1
System.out.println(new String(bytes, 0, len));//将字节数组转成String类型从0索引开始到fileInputStream.read(bytes)
}
}
for循环方法:
public static void getFor(FileInputStream fileInputStream) throws IOException {
byte[] bytes = new byte[1024];
for (int i; (i = fileInputStream.read(bytes)) != -1; ) {
System.out.println(new String(bytes, 0, i));
}
}
getWhile(fileInputStream);//调用while循环来遍历缓冲区字节
getFor(fileInputStream);//调用for循环来遍历缓冲区中的字节