Java高级_Day11(复制文件,字节缓冲流)

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();
    }

在这里插入图片描述

0123456789
hello\r\nwor
ld\r\nhello\r
\nworldllo\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();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值