zoukankan      html  css  js  c++  java
  • I/O流

    一、Properties 类 依赖于 ***.properties 文件中 (这个键值对的容器)
      构造方法:Properties();
      常用方法:Properties对象.put(key,value)//设置值
           Properties对象.getProperty(String key);//获取值
           Properties对象.load(InputStream is);//把 ***.properties 文件加载到 Properties对象中
           Properties对象.store(OutputStream out,String str(第二个参数值注释));//将Properties对象 输出到 ***.properties 文件中

    二、序列化流和反序列化流

      1、序列化流(ObjectOutputStream):
          构造方法:ObjectOutputStream(FileOutputStream fos)
          常用方法:writeObject(Object obj);

          

      2、返序列化流(ObjectIntputStream):
          构造方法:ObjectIntputStream(FileIntputStream fis)
          常用方法:reafObject()
        

       需要被序列化和反序列化的列必须 实现 Serializable 接口,不需要被序列化的字段可以用 transient 修饰 例如 (private transient int age;)

     1 public class Person implements Serializable {
     2     private String name;
     3     private int age;
     4     public String getName() {
     5         return name;
     6     }
     7     public void setName(String name) {
     8         this.name = name;
     9     }
    10     public int getAge() {
    11         return age;
    12     }
    13     public void setAge(int age) {
    14         this.age = age;
    15     }
    16     @Override
    17     public String toString() {
    18         return "Person [name=" + name + ", age=" + age + "]";
    19     }
    20     public Person(String name, int age) {
    21         super();
    22         this.name = name;
    23         this.age = age;
    24     }
    25     
    26     public Person() {
    27         super();
    28     }
    29     
    30 }
     1 //    序列化
     2     private void write() throws IOException{
     3         Person p = new Person("小红帽",18);
     4 //        明确数据源
     5         FileOutputStream fos = new FileOutputStream("D:\demo0723\Person.txt");
     6 //        创建 序列化流
     7         ObjectOutputStream oos = new ObjectOutputStream(fos);
     8         
     9 //        写入
    10         oos.writeObject(p);
    11         oos.close();
    12     }
    13     
    14 //    反序列化
    15     public static void read() throws IOException, ClassNotFoundException{
    16 //        明确数据源
    17         FileInputStream fis = new FileInputStream("D:\demo0723\Person.txt");
    18 //        创建返序列化流
    19         ObjectInputStream ois = new ObjectInputStream(fis);
    20 //        从 person.txt 中读取出来
    21 //        boolean  b  = 对象  instanceof  数据类型;
    22 //        Person P  = (Person)ois.readObject();
    23         
    24         boolean b = ois.readObject() instanceof Person;
    25         System.out.println(b);
    26         
    27 //        System.out.println(p);
    28         ois.close();
    29     }
  • 相关阅读:
    星时代曹波涛:分享一个测试数据生成的工具
    myabtis-plus 分页
    吴恩达机器学习_55过拟合问题/56代价函数
    吴恩达机器学习_51高级优化/52多元分类:一对多
    吴恩达机器学习_49代价函数/50简化代价函数与梯度
    吴恩达机器学习_46分类/47假设函数/48决策边界
    Windows10下安装VMware workstation 15.1虚拟机+配置Ubuntu16系统
    编程作业ex1:附加练习
    编程作业ex1:线性回归
    吴恩达机器学习_43矢量
  • 原文地址:https://www.cnblogs.com/yanghaoyu0624/p/11720765.html
Copyright © 2011-2022 走看看