Java高级_Day11(复制文件,字节缓冲流)
复制文件
/*
将D:\\IO\os\\窗里窗外.txt文档复制到当前module中
*/
public class FileCopy {
public static void main(String[] args) throws IOException {
// 实现读数据
FileInputStream fis = new FileInputStream("D:\\IO\\os\\窗里窗外.txt");
// 实现写数据
FileOutputStream fos = new FileOutputStream("day_11_IODemo\\copy\\窗里窗外.txt");
// 使用输入流来读数据 每次读取一个字节 读完之后 立即使用输出流将数据写出到目标文件
int len;
long begin = System.currentTimeMillis();// 获取开始 复制的时间
while((len = fis.read()) != -1){
fos.write(len);
}
long end = System.currentTimeMillis();
System.out.println("复制文件完成,共耗时:" + (end - begin) + "毫秒");//复制文件完成,共耗时:2189毫秒
// 释放资源
fos.close();
fis.close();
}
}
使用字节流读取数据(每次读取一个字节数组)
public class FileReadByteArray {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("D:\\IO\\os\\fos.txt");
// int read(byte[] b)
byte[] buff = new byte[10];
int len;
while( (len =fis.read(buff)) != -1){//每次读取10个字节 在去输出
// 将读取到的字节数组转换为字符串 输出
System.out.println(new String(buff));
}
fis.close();
}
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|
h | e | l | l | o | \r | \n | w | o | r |
l | d | \r | \n | h | e | l | l | o | \r |
\n | w | o | r | l | d | l | l | o | \r |
解决方法:
String(byte[] bytes, int offset, int length)
通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。
public class FileReadByteArray {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("D:\\IO\\os\\fos.txt");
// int read(byte[] b)
byte[] buff = new byte[10];
int len;
while( (len =fis.read(buff)) != -1){//每次读取10个字节 在去输出
// 将读取到的字节数组转换为字符串 输出转换的时候 读几个就转换几个
System.out.print(new String(buff,0,len));//
}
fis.close();
}
}
使用每次读取一个字节数组的方式 实现文件的复制:
/**
* @author Mr.Hu
* @create 2021/01/13
*
* 将D:\\IO\os\\窗里窗外.txt文档复制到当前module中 采用读取一个字节数组的方式
*
*/
public class FileCopyByByteArray {
public static void main(String[] args) throws IOException {
FileInputStream fis= new FileInputStream("d:\\IO\\os\\窗里窗外.txt");
FileOutputStream fos = new FileOutputStream("day_11_IODemo\\copy\\copy.txt");
byte[] buff = new byte[1024];
int len;
long begin= System.currentTimeMillis();
while((len = fis.read(buff))!= -1){
fos.write(buff,0,len);
}
long end = System.currentTimeMillis();
System.out.println("复制文件完成,共耗时:" + (end - begin) + "毫秒");
fos.close();
fis.close();
}
使用每次读取一个字节数组的方式 实现图片的复制:
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("D:\\IO\\os\\fos.txt");
// int read(byte[] b)
byte[] buff = new byte[10];
int len;
while( (len =fis.read(buff)) != -1){//每次读取10个字节 在去输出
// 将读取到的字节数组转换为字符串 输出转换的时候 读几个就转换几个
System.out.print(new String(buff,0,len));//
}
fis.close();
}
字节缓冲流(处理流)
BufferedInputStream
内部有一个缓冲区数组
当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次有多个字节
BufferedOutputStream 提供输出效率
//创建一个输出缓冲流 此处采用匿名对象创建一个OutputStream对象作为缓冲流的参数
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("buff.txt"));// 默认的缓冲区大小8192
bos.write("hello".getBytes());
bos.write("world".getBytes());//如果在写数据的时候 如果缓冲区没有写满,此时我们就必须强制刷新缓冲区 否则缓冲区的数据时不能写出到文件的
bos.flush();//强制刷新缓冲区
bos.close();// 在执行close之前会限执行flush
实现图片复制:
public static void main(String[] args) throws IOException {
// 创建一个输入缓冲流
InputStream in = new FileInputStream("d:\\IO\\os\\mn.jpg");
BufferedInputStream bis = new BufferedInputStream(in);
//创建一个输出缓冲流 此处采用匿名对象创建一个OutputStream对象作为缓冲流的参数
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("buff.jpg"));// 默认的缓冲区大小8192
byte[] buff = new byte[1024];
int len;
long begin= System.currentTimeMillis();
while((len = bis.read(buff)) != -1){
bos.write(buff,0,len);
}
long end = System.currentTimeMillis();
System.out.println("复制文件完成,共耗时:" + (end - begin) + "毫秒");//1
bos.close();
bis.close();
}
使用缓冲流实现复制视频:
public class CopyVido {
public static void main(String[] args) throws IOException {
long begin = System.currentTimeMillis();
//byteArrayCopy();//3306
buffStreamCopy();//1167
long end = System.currentTimeMillis();
System.out.println(end - begin);
}
// 每次读取一个字节数组
public static void byteArrayCopy() throws IOException {
FileInputStream fis = new FileInputStream("d:\\IO\\os\\Java高级- 23.avi");
FileOutputStream fos = new FileOutputStream("java23.avi");
byte[] buff = new byte[1024 * 1024] ;
int len ;
while((len = fis.read(buff)) != -1){
fos.write(buff,0,len);
}
fos.close();
fis.close();
}
// 使用字节缓冲流来实现
public static void buffStreamCopy() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\IO\\os\\Java高级- 23.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("java2323.avi"));
byte[] buff = new byte[1024 * 1024] ;
int len ;
while((len = bis.read(buff)) != -1){
bos.write(buff,0,len);
}
bis.close();
bos.close();
}