提高拷贝速度的解决方案
提高拷贝速度的解决方案
为了解决速度问题,字节流通过创建字节数组,可以一次读写多个数据。
一次读一个字节数组的方法:
public int read(byte[] b)
: 从输入流读取最多b.length个字节的数据- 返回的是读入缓冲区的总字节数,也就是实际的读取字节个数
FileInputStream fis = new FileInputStream("D:\\a.txt");
FileOutputStream fos = new FileOutputStream("D:\\b.txt");
byte[] bytes = new byte[1024];
int len; //本次读到的有效字节数---这次读了几个字节
while((len = fis.read(bytes)) != -1){
fos.write(bytes, 0, len);
}
fis.close();
fos.close();
byte[] 变量名 = "字符串".getBytes()
将字符串转换成字节数组。然后再调用fos输出流写入到文件中