IO流

IO流

一、字符流

  • FileReader和FileWriter

    *通过拷贝30M的视频来演示

  • import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class IODemo9_1 {
    public static void main(String[] args) {
        copy("F:\\新建文件夹\\aaa.mp4", "F:\\新建文件夹\\视频\\aaa.mp4");
    }
    
    private static void copy(String from, String to) {
        long time = System.currentTimeMillis();
        FileWriter fw = null;
        FileReader fr = null;
        try {
            fr = new FileReader(from);
            fw = new FileWriter(to);
            char[] buf = new char[512];
            int len = -1;
            while ((len = fr.read(buf)) != -1) {
                fw.write(buf, 0, len);
                fw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 可以将这两个方法提取成一个工具类
                fw.close();
                fr.close();
            } catch (IOException e) {
    
                e.printStackTrace();
            }
        }
        long time1 = System.currentTimeMillis() - time;
        System.out.println("耗时" + time1 / 1000);
    }
    }
    
  • 运行结果: 耗时29
    

    * BufferedReader : 增强读取性能的

    * BufferedWriter : 增强写性能的

        import java.io.BufferedReader;
        import java.io.BufferedWriter;
        import java.io.FileReader;
        import java.io.FileWriter;
        import java.io.IOException;
    
        public class IoDemo16_1 {
            public static void main(String[] args) {
                copy("F:\\新建文件夹\\aaa.mp4", "F:\\新建文件夹\\视频\\aaa.mp4");
            }
    
            private static void copy(String from, String to) {
                long time=System.currentTimeMillis();
                BufferedReader br=null;
                BufferedWriter bw=null;
                try {
                    br=new BufferedReader(new FileReader(from));
                    bw=new BufferedWriter(new FileWriter(to));
                    String len =null;
                    while((len=br.readLine())!=null){
                        bw.write(len);
                        bw.flush();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    try {
                        bw.close();
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                long time1=System.currentTimeMillis()-time;
                System.out.println("耗时"+time1/1000);
            }
        }
    

*运行结果

    耗时:23
* FileReader和BufferedReader的区别
  • FileReader:不能一行一行的读数据
  • BufferedReader : 可以一次读一行数据 速度比FileReader快

二、字节流

* FileInputStream : 读取文件

* FileOutputStream : 写文件

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;

    public class IODemo3_1 {
    public static void main(String[] args) {
        copy("F:\\新建文件夹\\aaa.zip", "F:\\新建文件夹\\视频\\a.zip");
    }

    private static void copy(String from, String to) {
        long time = System.currentTimeMillis();
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File file = new File(from);
            long length = file.length(); // 文件的大小
            long record = 0; // 用来记录复制了多少
            fis = new FileInputStream(file);
            fos = new FileOutputStream(to);
            byte[] buf = new byte[512];
            int len = -1;
            while ((len = fis.read(buf)) != -1) {
                fos.write(buf, 0, len);
                fos.flush();
                record += len;
                System.out.println("当前进度为:" + (record * 100 / length) + "%");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long time1 = System.currentTimeMillis() - time;
        System.out.println("耗时:" + time1 / 1000);
    }
    }

*运行结果

    当前进度为:99%
    当前进度为:99%
    当前进度为:100%
    耗时:26

* BufferedInputStream 增强读取性能的

* BufferedOutputStream 增强写性能的

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;

    public class IODemo4_1 {
    public static void main(String[] args) {
        copy("F:\\新建文件夹\\aaa.zip", "F:\\新建文件夹\\视频\\a.zip");
    }

    private static void copy(String from, String to) {
        long time = System.currentTimeMillis();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            File file = new File(from);
            long length = file.length();
            long record = 0; // 记录复制多少
            bis = new BufferedInputStream(new FileInputStream(file));
            bos = new BufferedOutputStream(new FileOutputStream(to));
            byte[] buf = new byte[512];
            int len = -1;
            while ((len = bis.read(buf)) != -1) {
                bos.write(buf, 0, len);
                record += len;
                System.out.println("当前进度为" + (record * 100) / length + "%");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long time1 = System.currentTimeMillis() - time;
        System.out.println("耗时:" + time1 / 1000);
    }
}

*运行结果

    当前进度为99%
    当前进度为99%
    当前进度为100%
    耗时:22

FileInputStream和BufferedInputStream的区别

  • FileInputStream是字节流 BufferedInputStream 是字符缓存流 使用BufferedInputStream读资源比使用FileInputStream读取的效率高

* InputStreamReader : 将字节码转换成字符码

* OutputStreamWriter : 将字符码转换成字节码

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.URL;
    public class TestDemo1_1 {
        public static void main(String[] args) throws IOException {
            URL url=new URL("http://www.baidu.com/");
            InputStream openStream = url.openStream();
            //将字节码转换成字符码
            InputStreamReader isr=new InputStreamReader(openStream, "UTF-8");
            char[] buf=new char[512];
            StringBuilder sb=new StringBuilder();
            int len=-1;
            while((len=isr.read(buf))!=-1){
                sb.append(buf, 0, len);
            }
            //将字符码转成字节码
            FileOutputStream fos=new FileOutputStream("index.html");
            OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
            osw.write(sb.toString().toCharArray());
            osw.flush();
            osw.close();
            System.out.println(sb.toString());
        }
    }

*文件的切割和合并

  • 文件的切割

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class TestDemo1_1 {
    public static void main(String[] args) {
        File file = new File("F:\\新建文件夹\\aaa.mp4");
        cutFile(file);
    }
    
    private static void cutFile(File file) {
        if (file.isDirectory()) {
            throw new IllegalAccessError("参数必须是文件");
        }
        long length = file.length();
        long singleLength = 1024 * 1024 * 10;
        int singlecount = (int) (length % singleLength == 0 ? length / singleLength : (length / singleLength) + 1);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            for (int i = 0; i < singlecount; i++) {
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(new File("F:\\新建文件夹\\视频\\", "part" + i + ".mp4"));
                    byte[] buf = new byte[512];
                    int len = -1;
                    int count = 0;
                    while ((len = fis.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                        fos.flush();
                        count += len;
                        if (count == 1024 * 1024 * 10) {
                            break;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    fos.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }
    }
    
  • 文件的合并

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FilenameFilter;
    import java.io.IOException;
    
    public class TestDemo2_1 {
    public static void main(String[] args) {
        File file = new File("F:\\新建文件夹\\视频\\");
        cpFile(file);
    }
    
    private static void cpFile(File file) {
        File[] listFiles = file.listFiles(new FilenameFilter() {
    
            @Override
            public boolean accept(File dir, String name) {
                // TODO Auto-generated method stub
                return name.startsWith("part");
            }
        });
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(file, "合并后.mp4"));
            for (int i = 0; i < listFiles.length; i++) {
                File file2 = listFiles[i];
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(file2);
                    byte[] buf = new byte[1024];
                    int len = -1;
                    while ((len = fis.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                        fos.flush();
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                } finally {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    }
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值