字节拷贝

public class Demo3_Copy {

    public static void main(String[] args) throws IOException {
        //demo1();
        //demo2();
        //demo3();
    }

    public static void demo3() throws FileNotFoundException, IOException {
        //第二种拷贝 不考虑 会导致内存溢出
        FileInputStream a =new FileInputStream("田馥甄 - 小幸运.mp3");//创建输入流对象 关联田馥甄 - 小幸运.mp3文件
        FileOutputStream b =new FileOutputStream("copy.mp3");//创建输出流对象 关联copy.mp3文件
        //int c =a.available();
        //System.out.println(c);
        
        byte [] arr =new byte[a.available()]; //创建于文件一样大小的文件数组
        a.read(arr);  //将文件上的字节读取到内存中
        b.write(arr);//将字节数组中的字节数据写到文件上
        a.close();//关流释放资源
        b.close();
    }

    public static void demo2() throws FileNotFoundException, IOException {
        //拷贝音乐 第一种拷贝太慢
        FileInputStream a =new FileInputStream("田馥甄 - 小幸运.mp3");//创建输入流对象 关联田馥甄 - 小幸运.mp3文件
        FileOutputStream b =new FileOutputStream("copy.mp3");//创建输出流对象 关联copy.mp3文件
        int c ;
        while ((c = a.read()) != -1) {//不断的读取每一个字节
            b.write(c);//将每一个字节写出
            
        }
        a.close();//关流释放资源
        b.close();
    }

    public static void demo1() throws FileNotFoundException, IOException {
        //拷贝照片
        FileInputStream a =new FileInputStream("IMG_0564.PNG");//创建输入流对象 关联IMG_0564.PNG文件
        FileOutputStream b =new FileOutputStream("copy.PNG");//创建输出流对象 关联copy.PNG文件
        int c ;
        while ((c = a.read()) != -1) {//不断的读取每一个字节
            b.write(c);//将每一个字节写出
            
        }
        a.close();//关流释放资源
        b.close();

    }public static void main(String[] args) throws IOException {
        //demo1();
        //demo2();
        //标准版
        FileInputStream a =new FileInputStream("xxx.txt");
        FileOutputStream c =new FileOutputStream("zzz.txt");
        byte [] arr =new byte[1024*8];//1024的整数倍
        int len;
        while ((len=a.read(arr))!=-1) {
            c.write(arr,0,len);
            
        }
        a.close();
        c.close();
        
        
    }

    public static void demo2() throws FileNotFoundException, IOException {
        FileInputStream a =new FileInputStream("xxx.txt");
        FileOutputStream c =new FileOutputStream("zzz.txt");
        byte [] arr =new byte[2];
        int len;
        while ((len=a.read(arr))!=-1) {
            c.write(arr,0,len);
            
        }
        a.close();
        c.close();
    }

    public static void demo1() throws FileNotFoundException, IOException {
        //第三种拷贝 定义小数组
                FileInputStream a =new FileInputStream("xxx.txt");
                byte [] arr =new byte[2];
                int b=a.read(arr);//将文件上的字节读取到自己数组中
                System.out.println(b);//读取到有效字节个数
                for (byte c : arr) {//获取到文件上的a和b
                System.out.println(c);    
                }
                System.out.println("------------");
                int d=a.read(arr);//将文件上的字节读取到自己数组中
                System.out.println(d);//读取到有效字节个数
                for (byte e : arr) {//获取到文件上的a和b
                System.out.println(e);    
                }
                a.close();
    }









//小数组拷贝

public static void main(String[] args) throws IOException {
        //demo1();
        //demo2();
        //标准版
        FileInputStream a =new FileInputStream("xxx.txt");
        FileOutputStream c =new FileOutputStream("zzz.txt");
        byte [] arr =new byte[1024*8];//1024的整数倍
        int len;
        while ((len=a.read(arr))!=-1) {
            c.write(arr,0,len);
            
        }
        a.close();
        c.close();
        
        
    }

    public static void demo2() throws FileNotFoundException, IOException {
        FileInputStream a =new FileInputStream("xxx.txt");
        FileOutputStream c =new FileOutputStream("zzz.txt");
        byte [] arr =new byte[2];
        int len;
        while ((len=a.read(arr))!=-1) {
            c.write(arr,0,len);
            
        }
        a.close();
        c.close();
    }

    public static void demo1() throws FileNotFoundException, IOException {
        //第三种拷贝 定义小数组
                FileInputStream a =new FileInputStream("xxx.txt");
                byte [] arr =new byte[2];
                int b=a.read(arr);//将文件上的字节读取到自己数组中
                System.out.println(b);//读取到有效字节个数
                for (byte c : arr) {//获取到文件上的a和b
                System.out.println(c);    
                }
                System.out.println("------------");
                int d=a.read(arr);//将文件上的字节读取到自己数组中
                System.out.println(d);//读取到有效字节个数
                for (byte e : arr) {//获取到文件上的a和b
                System.out.println(e);    
                }
                a.close();
    }


//缓冲区拷贝

public class Demo5_BufferCopy {

    public static void main(String[] args) throws IOException {
    //demo1();
    //flush和close的区别
    //close 方法具备刷新的方法  ,在关闭流之前就会刷新一次缓冲区 ,将缓冲区的字节全都刷新到文件上 在关闭
    //flush 具备刷新的功能 刷完之后还可以继续写  close刷完之后就不可以在写了
        BufferedInputStream a =new BufferedInputStream(new FileInputStream("xxx.txt"));
        BufferedOutputStream b =new BufferedOutputStream(new FileOutputStream("zzz.txt"));
        int num ;
        while ((num=a.read()) !=-1) {
            b.write(num);
            
        }
        /*a.close();
        b.close();*/
        b.flush();
    }

    public static void demo1() throws FileNotFoundException, IOException {
        //缓冲区的拷贝
            FileInputStream fis =new FileInputStream("田馥甄 - 小幸运.mp3");//创建输入流对象 关联田馥甄 - 小幸运.mp3文件
            FileOutputStream fos =new FileOutputStream("copy.mp3");//创建输出流对象 关联copy.mp3文件
            BufferedInputStream bis = new BufferedInputStream(fis);//创建缓冲区对象 对输入流进行包装 让其变的更加庞大
            BufferedOutputStream bos =new BufferedOutputStream(fos);//创建缓冲区对象 对输出流进行包装 让其变的更加庞大
            int b;
            while ((b=bis.read()) !=-1) {
                bos.write(b);
                
            }
            bis.close();
            bos.close();
    }






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值