栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

IO流学习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

IO流学习

再次学习IO流

文章目录
  • 前言
  • 一、IO流学习
  • 二、IO相关类
    • 1.FileInputStream类
    • 2.缓冲流(处理流)
  • 总结


前言

提示:系统性的学习IO流:

在Java中什么是IO流?IO流的用途是什么?


提示:以下是本篇文章正文内容,下面案例可供参考

一、IO流学习

IO流(输入流/输出流)分为字节流与字符流。
输入流与输出流是相对于程序而言。输入流是将本地或者其他终端文件读入到程序中。输出流是指将程序中的数据写入到本地或者其他终端。
根据操作方式将流分为字节流与字符流。
字节流:按照字节的方式操作数据源。
字符流:按照字符的方式操作数据源。
一般而言以stream结尾的类都是字节流。Readre结尾或者Writter结尾是字符流。
根据留里面的内容可以将流分为节点流与处理流。
节点流:流里面的内容是数据源。
处理流:流里面内容是节点流。

二、IO相关类 1.FileInputStream类

当我们对文件进行操作的时候,我们会使用字节流去处理。我们使用字节流处理的时候,在读入数据到程序时,会有两个read()方法,两个read方法返回值都是int,但表示的意义不同。并且读入的方式有区别。当我们对文件对象进行操作的时候,一定要记得关闭流对象。
代码如下(示例):

    private static void save1(String src, String des) {
        FileInputStream in = null;
        FileOutputStream out = null;
        //根据路径创建文件对象
        File file = new File(src);
        try {
            //创建输入流对象,将文件读取到程序
            in = new FileInputStream(file);

            //创建输出流对象,将文件数据从程序中写入到指定路径下的文件
            out = new FileOutputStream(des);

            //此时的value表示从文件中读取的一个字节内容,当读取到结尾的的时候,read()将会返回-1
            int value = 0;
            while ((value = in.read()) != -1) {
                out.write(value);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
private static void save2(String src, String des) {
        File file = new File(src);
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(file);
            out = new FileOutputStream(des);
            byte[] bytes = new byte[10];
            //此时的size表示的是我们的byte数组中实际存入数据的长度
            int size = 0;
            while ((size = in.read(bytes)) != -1) {
                out.write(bytes, 0, size);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
2.缓冲流(处理流)

使用处理流提高效率。
代码如下(示例):

private static void save3(String src, String des) {
        //使用处理流提高效率
        BufferedInputStream bin = null;
        BufferedOutputStream bout = null;
        try {
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(des);
            //在输入缓冲流底层存在一个缓冲数组,大小是8192B(8KB),在构造方法里面我们也可以传入自定义的大小
            //当我们使用缓冲流的时候,使用的byte数组大小小于8192时,我们每一次写入数据只会发生在填充满8192缓冲数组之后。
            //当我们使用缓冲流的时候,使用的byte数据大小大于8192时,缓冲流里面的缓冲数组失效。
            bin = new BufferedInputStream(in);
            bout = new BufferedOutputStream(out);
            int size = 0;
            byte[] bytes = new byte[1024];
            while ((size = bin.read(bytes)) != -1) {
                bout.write(bytes,0,size);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bin!=null){
                try {
                    bin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                bout.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (bout!=null){
                try {
                    bout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
总结

温故而知新,是一种理想状态。更多的是对已学习知识加深记忆与理解。多次的学习是提高自己编程技巧,查漏补缺的最佳方法。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/993319.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号