利用输入输出流复制文件练习

test1

import java.io.FileInputStream;
import java.io.FileOutputStream;

/*
    复制文件:
        数据源:D:\IU.jpg
            输入流:
                字节输入流:InputStream
                            - FileInputStream
        目的地:E:\李知恩.jpg
            输出流:
                字节输出流:OutputStream
                            - FileOutputStream
 */
public class CopyFileTest1 {
    public static void main(String[] args) throws Exception {
        //创建字节输入流对象
        FileInputStream fis = new FileInputStream("D:\\IU.jpg");
        //创建字节输出流对象那个
        FileOutputStream fos = new FileOutputStream("E:\\李刚的第二个对象.jpg");

        // 方式1:一次读写一个字节
//        int i = 0;
//        while ((i=fis.read())!=-1){
//            fos.write(i);
//        }


        // 方式2:一次读写一个字节数组
        byte[] bytes = new byte[1024];
        int length = 0;
        while ((length=fis.read(bytes))!=-1){
            fos.write(bytes,0,length);
        }


        //释放资源
        fos.close();
        fis.close();


    }
}

test2

import java.io.*;

/*
    比较4种复制读写的速度
    数据源:E:\bigdata32\32期day16 字节流 1.mp4


 */
public class CopyFileTest2 {
    public static void main(String[] args) {
        //1、普通字节流一次读写一个字节
//        fun1(); // > 10min 2231484
        //3、字节缓冲流一次读写一个字节
//        fun2(); //2834749

        //2、普通字节流一次读写一个字节数组
//        fun3(); // 共消耗 707 毫秒

        //4、字节缓冲流一次读写一个字节数组
        fun4(); // 共消耗 502 毫秒

    }

    public static void fun4(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //创建普通字节输入流对象
            bis = new BufferedInputStream(new FileInputStream("E:\\bigdata32\\32期day16 字节流 1.mp4"));
            //创建普通字节输出流对象那个
            bos = new BufferedOutputStream(new FileOutputStream("D:\\a4.mp4"));

            byte[] bytes = new byte[1024];
            int length = 0;

            long start = System.currentTimeMillis();
            while ((length= bis.read(bytes))!=-1){
                bos.write(bytes,0,length);
                bos.flush();
            }

            long end = System.currentTimeMillis();
            System.out.println("共消耗 "+(end-start)+" 毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void fun3(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //创建普通字节输入流对象
            fis = new FileInputStream("E:\\bigdata32\\32期day16 字节流 1.mp4");
            //创建普通字节输出流对象那个
            fos = new FileOutputStream("D:\\a3.mp4");

            byte[] bytes = new byte[1024];
            int length = 0;
            long start = System.currentTimeMillis();
            while ((length=fis.read(bytes))!=-1){
                fos.write(bytes,0,length);
            }

            long end = System.currentTimeMillis();
            System.out.println("共消耗 "+(end-start)+" 毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //字节缓冲流一次读写一个字节
    public static void fun2(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //创建普通字节输入流对象
            bis = new BufferedInputStream(new FileInputStream("E:\\bigdata32\\32期day16 字节流 1.mp4"));
            //创建普通字节输出流对象那个
            bos = new BufferedOutputStream(new FileOutputStream("D:\\a2.mp4"));

            int i = 0;

            int i2 = 1;
            long start = System.currentTimeMillis();
            while ((i=bis.read())!=-1){
                System.out.println(i2++);
                bos.write(i);
                bos.flush();
            }

            long end = System.currentTimeMillis();
            System.out.println("共消耗 "+(end-start)+" 毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //普通字节流一次读写一个字节
    public static void fun1() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //创建普通字节输入流对象
            fis = new FileInputStream("E:\\bigdata32\\32期day16 字节流 1.mp4");
            //创建普通字节输出流对象那个
            fos = new FileOutputStream("D:\\a1.mp4");

            int i = 0;

            int i2 = 1;
            long start = System.currentTimeMillis();
            while ((i=fis.read())!=-1){
                System.out.println(i2++);
                fos.write(i);
            }

            long end = System.currentTimeMillis();
            System.out.println("共消耗 "+(end-start)+" 毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

test3

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/*
    把java/src/com/shujia/day16/a4.txt内容复制到java/src/com/shujia/day16/b1.txt中
    数据源:java/src/com/shujia/day16/a4.txt
        字符输入流:
            Reader:
                - InputStreamReader
    目的地:java/src/com/shujia/day16/b1.txt
        字符输出流:
            Writer:
                - OutputStreamWriter

 */
public class CopyFileTest3 {
    public static void main(String[] args) throws Exception {
        // 创建字符输入流对象
        InputStreamReader isr = new InputStreamReader(new FileInputStream("java/src/com/shujia/day16/a4.txt"));
        //创建字符输出流对象
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("java/src/com/shujia/day16/b2.txt"));

        //一次读写一个字符
//        int i = 0;
//        while ((i = isr.read()) != -1) {
//            osw.write(i);
//            osw.flush();
//        }

        //一次读写一个字符数组
        char[] chars = new char[1024];
        int length = 0;
        while ((length=isr.read(chars))!=-1){
            osw.write(chars,0,length);
            osw.flush();
        }


        //释放资源
        osw.close();
        isr.close();
    }
}

test4

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class CopyFileTest4 {
    public static void main(String[] args) throws Exception {
        //创建字符缓冲输入流对象
        BufferedReader br = new BufferedReader(new FileReader("java/src/com/shujia/day16/b3.txt"));
        //创建字符缓冲输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("java/src/com/shujia/day16/c3.txt"));

        //1、一次读写一个字符
//        int i = 0;
//        while ((i=br.read())!=-1){
//            bw.write(i);
//            bw.flush();
//        }

        //2、一次读写一个字符数组
//        char[] chars = new char[1024];
//        int length = 0;
//        while ((length = br.read(chars)) != -1) {
//            bw.write(chars, 0, length);
//            bw.flush();
//        }


        //3、一次读写一行
        String line = null;
        while ((line= br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }


        //释放资源
        bw.close();
        br.close();

    }
}
posted @   rrrzzzrrr  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示