菜鸟初学IO流(2)

   字节流功能流

     基本数据类型流 |Data流 :可以传输数据+数据类型(基本数据类型+字符串)

      DataInputStream : 存在新增方法   readXxx();

      DataOutputStream :存在新增方法   writeXxx();

      Xxx为数据类型

      注意:读入的顺序要与写出的顺序保持一致

   

public static void main(String[] args) throws IOException {
    readFormFile("D://data2.txt");
}
//读入
public static void readFormFile(String path) throws IOException {
    //1.定义输入流
    DataInputStream in = new DataInputStream(new FileInputStream(path));
    //2.读取
    int i = in.readInt();
    boolean flag = in.readBoolean();
    char ch = in.readChar();
    String str = in.readUTF();
    //3.处理数据
    System.out.println(i);
    System.out.println(flag);
    System.out.println(ch);
    System.out.println(str);
    //4.关闭
    in.close();
}
//写出
public static void writeToFile(String path) throws IOException {
    //1.定义输出流
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
    //2.准备数据
    int i = 100;
    boolean flag = false;
    char ch = 'a';
    String msg = "哈哈";
    //3.写出
    out.writeInt(i);
    out.writeBoolean(flag);
    out.writeChar(ch);
    out.writeUTF(msg);
    //4.刷出
    out.flush();
    //5.关闭
    out.close();
}

字节流功能流 :
    对象流|Object流 : 传输数据+以及类型
    序列化: 把对象数据转为可传输或者可存储的状态的过程
    反序列化
    反序列化输入流 ObjectInputStream  --> Object readObject()
    序列化输出流   ObjectOutputStream  --> void writeObject(Object obj)
注意:
    1.先序列化后反序列化
    2.不是所有类型的对象数据都能序列化,要求类型必须实现一个空接口 Serializable
    3.不是所有的属性都需要序列化,可以通通过transient修饰
    4.静态成员不会序列化
    5.如果父类实现了序列化 ,子类没有实现,子类对象可以序列化所有内容
    5.如果父类没有实现序列化 ,子类实现了序列化,子类对象只能序列化子类的内容,父类的内容默认值
public static void main(String[] args) throws IOException, ClassNotFoundException {
        testWrite("D://obj.txt");
        testRead("D://obj.txt");
    }

    //读入
    public static void testRead(String path) throws IOException, ClassNotFoundException {
        //1.定义输入流
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));

        //2.读入
        int[] arr = (int[]) in.readObject();
        Person p = (Person) in.readObject();

        //3.处理
        System.out.println(Arrays.toString(arr));
        System.out.println(p);

        //4.关闭
        in.close();
    }

    //写出
    public static void testWrite(String path) throws IOException {
        //1.定义输出流
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path));

        //2.准备数据
        int[] arr = {1,2,3,4,5};
        Person p = new Person(1001,"张三",18);

        //3.写出
        out.writeObject(arr);
        out.writeObject(p);

        //4.刷出
        out.flush();

        //.关闭
        out.close();

        p.setAge(20);
    }
}

class Person implements Serializable {
    private int id;
    private String name;
    //private transient int age;
    private static int age;

    public Person() {
    }

    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值