zoukankan      html  css  js  c++  java
  • JAVA基础——对象流

    对象的输入输出流的作用: 用于写入对象 的信息和读取对象的信息。 使得对象持久化。    ObjectInputStream   : 对象输入流    ObjectOutPutStream  :对象输出流 

    简单的实例

    复制代码
     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 import java.io.ObjectInputStream;
     6 import java.io.ObjectOutputStream;
     7 import java.io.Serializable;
     8 
     9 //创建要写入磁盘的类,这个类需要实现接口 Serializable(可系列化的)
    10 class Student implements Serializable{
    11     
    12     // 在这里保证了serialVersionUID 的唯一性,防止属性变量的临时改变,从而造成写入id与读取id不同
    13     private static final long serialVersionUID = 1L; 
    14     int id ; //额外需要添加一个属性
    15     
    16     String name ;
    17     transient String sex; //transient修饰属性,表示暂时的,则这个属性不会被写入磁盘
    18     transient int age;
    19     
    20     public Student(String name,String sex,int age){
    21         this.name = name;
    22         this.sex = sex;
    23         this.age = age;
    24     }
    25 }
    26 
    27 
    28 public class objectIO {
    29 
    30     /**
    31      * @param args
    32      * @throws IOException 
    33      * @throws ClassNotFoundException 
    34      */
    35     public static void main(String[] args) throws IOException, ClassNotFoundException {
    36         // TODO Auto-generated method stub
    37 
    38         createObj();
    39         readObj();
    40     }
    41     
    42     //(一)先写入对象
    43     public static void createObj() throws IOException {
    44         //1.创建目标路径
    45         File file = new File("C:\Users\bg\Desktop\objTest.txt");
    46         //2.创建流通道
    47         FileOutputStream fos = new FileOutputStream(file);
    48         //3.创建对象输出流
    49         ObjectOutputStream objOP = new ObjectOutputStream(fos);
    50         //4.创建类对象,并初始化
    51         Student stu = new Student("玛丽苏", "男", 18);
    52         //5.向目标路径文件写入对象
    53         objOP.writeObject(stu);
    54         //6.关闭资源
    55         objOP.close();
    56     }
    57     
    58     //再读取对象
    59     public static void readObj() throws IOException, ClassNotFoundException {
    60         File file = new File("C:\Users\bg\Desktop\objTest.txt");
    61         FileInputStream fis = new FileInputStream(file);
    62         ObjectInputStream objIP = new ObjectInputStream(fis);
    63         //读取对象数据,需要将对象流强制转换为 要写入对象的类型
    64         Student stu = (Student)objIP.readObject();
    65         System.out.println("
     name:"+stu.name+"
     sex:"+stu.sex+"
     age:"+stu.age);
    66         objIP.close();  
    67     }
    68 
    69 }
    复制代码

    打印效果

     name:玛丽苏
     sex:null       //后面的这连个属性使用了 transient修饰   
     age:0

    用到方法:writeObject(Object o);  //向磁盘写入对象

         readObject();  //读取磁盘的对象,注意这里需要强制类型

    对象输入输出流的使用注意点:        1.如果想将一个对象写入到磁盘中,那么对象所属的类必须要进行序列化,实现Serializable 接口,Serializable接口没有任何方法 ,是一个标记接口        2.如果对象所属的类的成员变量发生改变,你在读取原来的对象是就会报错,如果想要解决报错,保证serialVersionUID是唯一。        3.如果你不想将某些信息存入到磁盘 就可以同过transient关键字修饰成员变量        4.如果一个类中引用了另外的一个类,那么另外的这个类也要实现Serializable接口。

    如果:

      

  • 相关阅读:
    CodeForces 659F Polycarp and Hay
    CodeForces 713C Sonya and Problem Wihtout a Legend
    CodeForces 712D Memory and Scores
    CodeForces 689E Mike and Geometry Problem
    CodeForces 675D Tree Construction
    CodeForces 671A Recycling Bottles
    CodeForces 667C Reberland Linguistics
    CodeForces 672D Robin Hood
    CodeForces 675E Trains and Statistic
    CodeForces 676D Theseus and labyrinth
  • 原文地址:https://www.cnblogs.com/huan-guo/p/8547436.html
Copyright © 2011-2022 走看看