I/O(Input/Output)流

一、IO介绍以及分类

        IO(Input/Output)流是计算机程序中用来处理输入和输出数据的抽象概念。在Java中,IO流分为两种类型:字节流和字符流。

        IO流的分类:

        1、 根据处理的数据类型不同可以分为:字符流和字节流。

        2、根据数据的流向不同可以分为:输入流和输出流。

  • 字节流(Byte Streams)

    • InputStream 和 OutputStream 是所有字节流的抽象基类,分别用于从各种数据源读取字节和向各种目标写入字节。
    • FileInputStream 和FileOutputStream 用于与文件进行字节流的输入和输出。
    • BufferedInputStream 和 BufferedOutputStream 提供了缓冲功能,可以提高读写效率。
    • 其他类似 DataInputStream 和 DataOutputStream 可以用于读写基本数据类型的二进制表示。
  • 字符流(Character Streams)

    • Reader 和 Writer 是所有字符流的抽象基类,用于从各种数据源读取字符和向各种目标写入字符。
    • FileReader 和 FileWriter 用于与文件进行字符流的输入和输出。
    • BufferedReader 和 BufferedWriter 提供了缓冲功能,可以提高读写效率。
    • InputStreamReader 和 OutputStreamWriter 可以将字节流转换为字符流,从而使得可以使用字符流处理字节数据。

        选择建议:

  1. 如果你需要处理文本文件,推荐使用字符流,因为它们能够更好地处理字符编码和文本数据。
  2. 如果需要处理二进制文件或者未经处理的数据,应该使用字节流。

二、字符流

@Test
public void test1() {
    FileReader fileReader = null;
    try {
        //空指针异常
        //fileReader = new FileReader("io1.txt");
        fileReader = new FileReader("io.txt");
        //Reads a single character.
        int ch1 = fileReader.read();
        System.out.println((char) ch1);//a
        int ch2 = fileReader.read();
        System.out.println((char) ch2);//b
        int ch3 = fileReader.read();
        System.out.println((char) ch3);//c
        int ch4 = fileReader.read();
        System.out.println(ch4);//-1
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {//在finally里面关闭io流
        if (fileReader != null) {
            try {
                fileReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
@Test
public void test2() {
    FileReader fileReader = null;
    try {
        fileReader = new FileReader("io.txt");
        int ch = -1;
        //while循环去读,直到ch的值等于-1,就退出循环
        while ((ch = fileReader.read()) != -1) {
            System.out.println((char) ch);
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {//在finally里面关闭io流
        if (fileReader != null) {
            try {
                fileReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
@Test
public void test3() {
    try {
        FileReader fileReader = new FileReader("io.txt");
        char[] buffer = new char[10];
        int length = -1;
        //public int read(char[] cbuf)
        //一次将10字符读入到buffer数组里面
        //返回:读取的字符数,如果已经达到流的末尾,返回-1
        while ((length = fileReader.read(buffer)) != -1) {
            System.out.println(length);
            System.out.println(buffer);
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (fileReader != null) {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
@Test
public void test4() {
    FileReader fileReader = null;
    FileWriter fileWriter = null;
    try {
        fileReader = new FileReader("io.txt");
        fileWriter = new FileWriter("io_back.txt");
        char[] buffer = new char[5];
        int length = -1;
        //public int read(char[] cbuf)
        //一次将10字符读入到buffer数组里面
        //返回:读取的字符数,如果已经达到流的末尾,返回-1
        while ((length = fileReader.read(buffer)) != -1) {
            System.out.println(length);
            System.out.println(buffer);
            //读出多少写多少,最后一次读出来的数据很有可能不够buffer数组的长度
            fileWriter.write(buffer, 0, length);
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        //先打开的后关闭
        if (fileWriter != null) {
            try {
                fileReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
        if (fileReader != null) {
            try {
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

三、字节流

@Test
public void test5() {
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        fileInputStream = new FileInputStream("ten.jpg");
        fileOutputStream = new FileOutputStream("ten_back.jpg");
        byte[] buffer = new byte[1024];
        int length = -1;
        while ((length = fileInputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, length);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fileInputStream != null) {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

四、对象流

        ObjectInputStream、ObjectOutputStream

        将对象写入文件的操作流ObjectOutputStream,称为序列化

        从流中读取对象的操作流程ObjectInputStream,称为反序列化

        java.io.NotSerializableException: com.situ.day13.Student

        Serialize:序列化

        一般情况下:先打开的后关闭,后打开的先关闭        

        另一种情况:看依赖关系,如果流A依赖于流B,先关闭流A,再关闭流B

public class Student implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
    private String gender;

    public Student() {
    }

    public Student(Integer id, String name, Integer age, String gender) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}

@Test
public void test6() {
    Student student = new Student();
    student.setId(1);
    student.setAge(2);
    student.setGender("男");
    student.setName("张三");

    ObjectOutputStream objectOutputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream("stu");
        objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(student);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }finally {
        if (objectOutputStream != null){
            try {
                objectOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if (fileOutputStream != null){
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

@Test
public void test7() {
    ObjectInputStream objectInputStream = null;
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream("stu");
        objectInputStream = new ObjectInputStream(fileInputStream);
        Student student = (Student) objectInputStream.readObject();
        System.out.println(student);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }finally {
        if (objectInputStream != null){
            try {
                objectInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if (fileInputStream != null){
            try {
                fileInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值