FileInputStream中read(byte[] b) 的使用讲解
文章目录
一、read(byte[] b) 方法
该方法一次可以读取多个字节,数量取决于数组长度
int read(byte[] b):一次最多读取b.length个字节。
作用:减少硬盘和内存的交互,提高程序的执行效率
读取方式:往byte[]数组当中读。
注意:这个方法的返回值是读取到的字节数量(不是字节本身)
二、代码展示
1.框架
框架很注重要,一定要会搭
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest03 {
public static void main(String[] args) {
FileInputStream fis =null;
try {
fis = new FileInputStream("temp");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.被读取文件展示
为了便于理解,文件中仅有a b c d e f五个英文小写字符
3.初步使用
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest03 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("temp");
// 开始读,采用byte数组,一次读取多个字节。最多读取“数组.length”个字节
byte[] bytes = new byte[5];//准备一个5个长度的Byte数组,一次最多读5个字节
// 这个方法的返回值是:读取到的字节数量(不是字节本身)
int readCount = fis.read(bytes);
System.out.println(readCount);//第一轮读到5个字节
readCount = fis.read(bytes);
System.out.println(readCount);//第二轮只读到1个字节
readCount = fis.read(bytes);
System.out.println(readCount);//未读取到字节,返回-1
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
执行结果如下:
可以看见,该方法一次就读取了5个字节(数组长度为5),第二次因为文件中只剩一个字节所以就只读到1个字节,当读到文件末尾时,无字节可读,所以返回-1。
但该方法的返回的值是读取到的字节数量,不是字节本身。
当我们想要读取字节本身时,我们可以用 String(byte[] bytes) 方法来将数组转换成字符串。
其中,bytes是所选中的数组名称。
4.转化为字符串
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/*
int read(byte[] b)
一次最多读取b.length个字节。
减少硬盘和内存的交互,提高程序的执行效率
往byte[]数组当中读。
*/
public class FileInputStreamTest03 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("temp");
// 开始读,采用byte数组,一次读取多个字节。最多读取“数组.length”个字节
byte[] bytes = new byte[5];//准备一个5个长度的Byte数组,一次最多读5个字节
// 这个方法的返回值是:读取到的字节数量(不是字节本身)
int readCount = fis.read(bytes);
System.out.println(readCount);//第一轮读到5个字节
System.out.println(new String(bytes));//abcde 将字节全部转换成字符串
readCount = fis.read(bytes);
System.out.println(readCount);//第二轮只读到1个字节
System.out.println(new String(bytes));//fbcde 但这种方法会覆盖数组之前的内容
readCount = fis.read(bytes);
System.out.println(readCount);//未读取到字节,返回-1
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
执行结果:
可以看见,第一轮的转换是没有问题的,但第二轮是在文件末尾,需要转换的只有“f”,但结果却是“fbcde”。
这是因为我们的操作是将被读取文件的数据放进数组中,第一轮读取时数组填满,而第二轮读取时,是将新的数据“覆盖”在数组上,过程如图所示:
未存入前:
第一轮:
第二轮:
第二轮只读到f,只覆盖了a,而byte数组中的bcde并未被清除,所以String(bytes)转换结果为fbcde。
因为String(bytes)是将byte数组中的所有字节都转为字符串,而我们只想要转换每轮读取到的字符,这时我们可以使用String(byte[] bytes, int offset, int length) 方法。
其中,int offset指数组的起始位置,int length指选取的长度。
5.正常转换
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/*
int read(byte[] b)
一次最多读取b.length个字节。
减少硬盘和内存的交互,提高程序的执行效率
往byte[]数组当中读。
*/
public class FileInputStreamTest03 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("temp");
// 开始读,采用byte数组,一次读取多个字节。最多读取“数组.length”个字节
byte[] bytes = new byte[5];//准备一个5个长度的Byte数组,一次最多读5个字节
// 这个方法的返回值是:读取到的字节数量(不是字节本身)
int readCount = fis.read(bytes);
System.out.println(readCount);//第一轮读到5个字节
// System.out.println(new String(bytes));abcde 将字节全部转换成字符串
System.out.println(new String(bytes,0,readCount));
//不应全部转换,应该读取了读取了多少个字节就转换多少个,所以采用该方法
readCount = fis.read(bytes);
System.out.println(readCount);//第二轮只读到1个字节
// System.out.println(new String(bytes));fbcde 但这种方法会覆盖数组之前的内容
System.out.println(new String(bytes,0,readCount));
readCount = fis.read(bytes);
System.out.println(readCount);//未读取到字节,返回-1
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
String(bytes,0,readCount):因为readCount的返回值是读取到的字节数量,而我们只需要转换读到的字符,所以readCount符合要求,用readCount填入选取长度位。
总结
本文章介绍了read(byte[] b) 方法的使用,解决了read()方法内存硬盘交互频繁、浪费时间和资源的问题。
为了讲解清晰,本文并未采取最简洁的代码,如有需要,可见下文:
https://blog.csdn.net/m0_57497737/article/details/118662595.