Java IO流(下)

字节流:

新建一个Student类:


import java.io.Serializable;


//Serializable 接口中没有任何方法需要实现,它就是一个标志
//表示这个类的对象可以进行序列化和反序列化的操作
//序列化:Java对象转化为byte[]数组(二进制数据)
//反序列化:二进制数据还原为Java对象
//保存数据的时候进行序列化操作,把Java对象转化为二进制数据保存到硬盘
//读取数据的时候进行反序列化操作,把硬盘里的
public class Student implements Serializable{
    private int number;
    private String name;
    private int score;


    public Student() {
        super();
    }
    public Student(int number, String name, int score) {
        super();
        this.number = number;
        this.name = name;
        this.score = score;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }

}

控制数据写入:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Application {

    public static void main(String[] args) {
        // 使用字节流完成对象的读写操作

        /*
         * 字节流:InputStream 和 OutputStream 两个抽象类是所有字节输入输出流的父类
         * 比较常用的子类有:
         *      FileInputStream 和FileOutputStream 读写文件内容(图片、视频、音频...)
         *      使用和字符流一样,只是用byte[]装载数据
         * ObjectInputStream 和 Object FileOutputStream  读写Java对象(Student,People...)
         */


        Student s1 = new Student(10010, "张三", 99);

        //想要把s1 保存到硬盘上,需要一以下几步
        /*
         * 1、Student 类 实现 Serializable 接口
         * 2、使用ObjectInnputStream保存Student 类对象
         */

        try {

            //把s1 保存到 333.dat文件中,这个文件系统会自动创建
            FileOutputStream fos = new FileOutputStream("S:/333.dat");

            //根据fos 创建用于保存对象的输出流oos
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            //保存s1到硬盘
            oos.writeObject(s1);

            //关闭流 哪个最后创建先关哪个
            oos.close();
            fos.close();
        } catch (IOException e) {//修改IO异常 导包

            e.printStackTrace();
        }

        //如果需要保存多个对象,可以把多个对象放入集合中,把集合保存到硬盘上
        Student s2 = new Student(10011,"lisi",90);
        Student s3 = new Student(10012,"liuliu",80);
        Student s4 = new Student(10013,"ergou",92);

        ArrayList<Student> students = new ArrayList<>();
        students.add(s2);
        students.add(s3);
        students.add(s4);

        try {
            FileOutputStream fos2 = new FileOutputStream("S:/666.txt");

            ObjectOutputStream oos2 = new ObjectOutputStream(fos2);

            //把集合保存到硬盘上
            oos2.writeObject(students);

            oos2.close();
            fos2.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

读取数据:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class Application2 {

    public static void main(String[] args) {

        //使用Object Input Stream 来读取硬盘上保存的Java对象

        try {
            FileInputStream fis = new FileInputStream("S:/333.dat");
            ObjectInputStream ois = new ObjectInputStream(fis);

            //读取保存的Java对象
            Student s1 = (Student) ois.readObject();

            System.out.println(s1.getNumber() + " " + s1.getName() + " " + s1.getScore());

            ois.close();
            fis.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //读取集合
        try {
            FileInputStream fis2 = new FileInputStream("S:/666.txt");
            ObjectInputStream ois2 = new ObjectInputStream(fis2);

            //强制转化时需要注意:存的是什么,取出来就是什么
            ArrayList<Student> students = (ArrayList<Student>) ois2.readObject();

            for (Student student : students) {
                System.out.println(student.getNumber() + " " +student.getName() + " " + student.getScore());
            }

            //关流
            ois2.close();
            fis2.close();

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

    }

}

猜你喜欢

转载自blog.csdn.net/ilovehua521/article/details/82687869