• JavaSE之IO流


    前导

    数据存在的内存中的问题:

    不能永久化存储,只要代码运行结束,所有数据都会丢失。

    所以想永久存储数据要将数据存储在文件中。

    IO流的概念

    I表示intput,输入。

    O表示output,输出。

    IO流的作用

    将数据写到文件中,实现数据永久化存储

    读取文件中的数据到程序

    IO的分类

    按流向分 (站在程序的角度)

    输入流

    输出流

    按操作数据类型分

    字节流:
    操作所有类型的文件

    字符流
    只能操作纯文本文件

    什么是纯文本文件?
    用windows记事本打开能读的懂,就是纯文本文件。

    IO顶级父类都是抽象的

    字节流写数据

    /*
    目标:字节流写一个字节数据(重点)
    
    FileOutputStream构造器
        FileOutputStream(String name) 创建文件字节输出流,让程序和指定文件建立管道
        FileOutputStream(File file) 创建文件字节输出流,让程序和指定文件建立管道
    
    OutputStream写数据的方法
        void write(int b) 写一个字节到流中
        void write(byte[] b) 写字节数组到流中
        void write(byte[] b, int off, int len) 写字节数组的一部分到流中
    
    
    注意:
        1.IO流的路径不能写文件夹              FileNotFoundException   study_day10\abc (拒绝访问。)
        2.父路径要存在                        FileNotFoundException   study_day10\1.txt (系统找不到指定的路径。)
        3.使用相对路径,模块要放在项目里面
     */
    public class Demo13 {
        public static void main(String[] args) throws IOException {
            //使用IDEA或记事本等文本编辑器,会认为我们想看文字,自动将字节转为字符
    
            // 1.创建文件字节输出流 如果文件不存在就创建
            FileOutputStream outputStream = new FileOutputStream("study_day10\\abc\\2.txt");
            // 2.写数据
            // void write(int b) 写一个字节到流中
            outputStream.write(97);
            // void write(byte[] b) 写数组所有字节到流中
            //byte[] bytes = {98,99,66,77,88};
            //outputStream.write(bytes);
            // void write(byte[] b, int off, int len) 写数组部分字节到流中
            // byte[] b: 要写出数据的数组
            // int off: 从数组哪个索引开始写
            // int len: 写多少个
            //outputStream.write(bytes,0,2);
            // 3.关闭
            outputStream.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    字节流读数据

    /*
    目标:学习FileInputStream从文件一次读取一个字节
    
    FileInputStream构造器
        FileInputStream(String name)
        FileInputStream(File file)
    
    FileInputStream读数据方法
        int read() 从输入流读取一个字节,返回读取的字节数据
        int read(byte b[]) 从输入流读取一个字节数组,返回读取的数量
    
     */
    public class Demo14 {
        public static void main(String[] args) throws IOException {
            // 1.创建文件字节输入流
            FileInputStream fileInputStream = new FileInputStream("study_day10\\abc\\1.txt");
    
            // 2. int read() 读取一个字节数据,返回值就是读取的字节数据
            int read = fileInputStream.read();
            System.out.println((char) read);
    
            //第二次读就是读下一个字节
            int read1 = fileInputStream.read();
            System.out.println((char)read1);
    
            //没有数据读到的就是-1
    
            //全部一个一个读出来太久了,用循环
            int b;
            while((b = fileInputStream.read())!=-1){
                System.out.println((char) b);
            }
    
            // 3.关闭
            fileInputStream.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    /*
    目标:学习字节流一次读取一个字节数组的数据
        int read(byte b[]) 读取多个字节数据,读取的字节数据保存在数组中,返回读取的数量
     */
    public class Demo16 {
        public static void main(String[] args) throws IOException {
            // 文件中的数据   abcdefg
            FileInputStream inputStream = new FileInputStream("study_day10\\abc\\1.txt");
    
            byte[] bytes = new byte[3];
    
            //读多个数据时,读取的数据在数组中
            //返回的len是读到的数量
            int len = inputStream.read(bytes);
    
            System.out.println(len);
            String s = new String(bytes,0,len);
            System.out.println(s);
    
            //没有数据的时候为-1
    
            inputStream.close();
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    /*
    目标:使用字节流将 "study_day10/abc/xyz.png" 复制到 "D:/MyFileTest/xyz.png"
     */
    public class Demo17 {
        public static void main(String[] args) throws IOException {
            // 1.创建文件字节输入流
            FileInputStream inputStream = new FileInputStream("study_day10/abc/xyz.png");
    
            // 2.创建文件字节输出流
            FileOutputStream outputStream = new FileOutputStream("D:/MyFileTest/xyz.png");
    
            // 3.循环读写
            byte[] bytes = new byte[1024 * 8];
            int len;
            while((len = inputStream.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
            }
    
            // 4.关闭资源 从下往上看,先开的后关
            outputStream.close();
            inputStream.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    字符流读数据

    public static void main(String[] args) throws IOException {
            // 1.创建文件字符输入流
            FileReader reader = new FileReader("study_day10\\abc\\3.txt");
            // 2.读取一个字符
            int read = reader.read();
    
            // 2.读取多个字符
            char[] c = new char[3];
            int len;
            while((len = reader.read(c))!=-1){
                String s = new String(c, 0, len);
                System.out.println(s);
            }
            // 3.关闭
            reader.close();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    字符流写数据

    /*
    目标:学习Writer字符流写数据
        void write(int c) 写一个字符
        void write(char[] cbuf) 写一个字符数组到流中
        void write(char[] cbuf, int off, int len) 写一个字符数组的一部分到流中
        void write(String str) 写一个字符串到流中
        void write(String str, int off, int len) 写一个字符串的一部分到流中
     */
    public class Demo191 {
        public static void main(String[] args) throws IOException {
            // 创建文件字符输出流
            FileWriter writer = new FileWriter("study_day10\\abc\\3.txt");
            // void write(int c)
            writer.write(97);
            // void write(char[] cbuf)
            char[] c = {'1','2','3'};
            writer.write(c);
            // void write(char[] cbuf, int off, int len)
            writer.write(c,0,2);
            // void write(String str)
            writer.write("我");
            // void write(String str, int off, int len)
            writer.write("可以给我",0,2);
            writer.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    换行和追加

    /*
    目标:数据追加续写和换行
    
        FileWriter(String name) 文件不存在会创建文件, 会覆盖以前的数据
        FileWriter(File file) 文件不存在会创建文件, 会覆盖以前的数据
    
    追加续写: 在已有数据上完后添加数据
        FileWriter(String name, boolean append) append为true,在已有内容后面接着写数据
        FileWriter(File file, boolean append) append为true,在已有内容后面接着写数据
    
    换行:
        \r\n: 表示换行
     */
    public class Demo20 {
        public static void main(String[] args) throws IOException {
            // 获取文件对象;
            /*FileOutputStream fis = new FileOutputStream("study_day10\\abc\\3.txt");
            fis.write(65);*/
            FileWriter fiw = new FileWriter("study_day10\\abc\\3.txt", true);
            fiw.write(65);
            // 换行
            fiw.write("\r\n");
            fiw.write(66);
    
            // 追加续写
    
    
            fiw.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    最后

    如果你对本文有疑问,你可以在文章下方对我留言,敬请指正,对于每个留言我都会认真查看。

  • 相关阅读:
    Typora基础篇
    Ubuntu中安装R语言环境并在jupyter kernel里面增加R kernel
    基于空间特征选择的水下目标检测方法
    WPF——样式与模板
    leetcode算法题--机器人的运动范围
    理解JS的三座大山
    函数(2)
    ubuntu docker 部署 vue 项目
    opencv-python之位平面分解与数字水印
    产品思维训练 | 如何让更多人用支付宝点外卖?
  • 原文地址:https://blog.csdn.net/weixin_47543906/article/details/127820984