欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 对象序列化 properties IO流

程序员文章站 2024-03-07 14:41:03
...

序列化需要显示的给于***
不需要序列化的变量可以用transient修饰,成员变量会给与默认值
properties与流有一定关系,在此一并汇总出来。
java 对象序列化 properties IO流
java 对象序列化 properties IO流

'对象的类需要实现序列化接口'
'有一个***'

public class Student  implements Serializable{    
    //给定一个序列化号,即使类信息修改了也会成功的反序列化
    private static final long serialVersionUID = 44L;
    private String name;//姓名
    // 加上transient不被序列化
    // 序列化后由于是成员变量给与默认值0
    private transient  int age;//年龄,

'序列化对象'
'oos.writeObject(s)写入对象S'

package com.itheima_03;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/*
    对象序列化流
        构造方法:
            ObjectOutputStream​(OutputStream out):
            创建一个写入指定的OutputStream的ObjectOutputStream

        序列化对象的方法:
            void writeObject​(Object obj):将指定的对象写入ObjectOutputStream

    NotSerializableException:抛出一个实例需要一个Serializable接口。 
    序列化运行时或实例的类可能会抛出此异常

    类的序列化由实现java.io.Serializable接口的类启用。 
    不实现此接口的类将不会使任何状态序列化或反序列化
 */
public class ObjectOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        //ObjectOutputStream​(OutputStream out):
       // 创建一个写入指定的OutputStream的ObjectOutputStream
        ObjectOutputStream oos = new ObjectOutputStream(
        new FileOutputStream("F:\\生活\\a.txt"));
        Student s = new Student("002","林青霞",18,"香港");
        oos.writeObject(s);//序列化对象
        oos.close(); 
        ObjectInputStream ois = new ObjectInputStream(
        new FileInputStream("F:\\生活\\a.txt"));
        Student stu1 = (Student) ois.readObject();//反序列化到内存
        System.out.println(stu1);
        ois.close();
    }
}

序列化需要面对的问题
java 对象序列化 properties IO流

exception in thread "main" java.io.InvalidClassException: a1.Student;
local class incompatible: 
stream classdesc serialVersionUID = -5278967030564897865,
local class serialVersionUID = -8627561233132897662

两个serialVersionUID不一致
'给定一个序列化号'
'不想被序列化的可以transient,会有默认值'


public class Student  implements Serializable{    
    //给定一个序列化号,即使类信息修改了也会成功的反序列化
    private static final long serialVersionUID = 445114L;
    private String name;//姓名
    // 加上transient不被序列化
    // 序列化后由于是成员变量给与默认值0
    private transient  int age;//年龄,

properties

Properties概述:
	是一个Map体系的集合类
	Properties可以保存到流中或从流中加载
	属性列表中的每个键及其对应的值都是一个字符串,创建对象的时候就不需要写上泛型了

	练习: Properties作为Map集合的使用
'简单 存入与读取'

public static void main(String[] args) throws Exception{
        Properties p = new Properties();
        p.put("001", "aa");
        p.put("002", "bb");
        p.put("003", "cc");
        Set<Object> keySet = p.keySet();
        for (Object key : keySet) {
           Object property = p.get(key);
            System.out.println(property);
        }

    }

特有方法
java 对象序列化 properties IO流

package com.itheima_04;

import java.util.Properties;
import java.util.Set;

/*
    Properties作为集合的特有方法:
        Object setProperty(String key, String value):设置集合的键和值,都是String类型,底层调用Hashtable方法put
        String getProperty(String key):使用此属性列表中指定的键搜索属性
        Set<String> stringPropertyNames():从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
 */
public class PropertiesDemo02 {
    public static void main(String[] args) {
        //创建集合对象
        Properties prop = new Properties();

        //Object setProperty(String key, String value):设置集合的键和值,都是String类型,底层调用Hashtable方法put
        prop.setProperty("itheima001", "林青霞");
        /*
            Object setProperty(String key, String value) {
                return put(key, value);
            }

            Object put(Object key, Object value) {
                return map.put(key, value);
            }
         */
        prop.setProperty("itheima002", "张曼玉");
        prop.setProperty("itheima003", "王祖贤");

        //String getProperty(String key):使用此属性列表中指定的键搜索属性
//        System.out.println(prop.getProperty("itheima001"));
//        System.out.println(prop.getProperty("itheima0011"));

//        System.out.println(prop);

        //Set<String> stringPropertyNames():从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
        Set<String> names = prop.stringPropertyNames();
        for (String key : names) {
//            System.out.println(key);
            String value = prop.getProperty(key);
            System.out.println(key + "," + value);
        }
    }
}

properties 和IO流结合的方法

集合与文件的集成式的读取load、保存store。

java 对象序列化 properties IO流

public static void main(String[] args) throws IOException {
        Properties p = new Properties();
        p.setProperty("001", "aaa");
        p.setProperty("002", "bbb");
        p.setProperty("003", "ccc");
        //将集合中的数据存入到文件中农,comments项在文件的最上端,同时还有时间信息
        p.store(new FileWriter("F:\\生活\\a.txt"), " mapData 存入文件");

        System.out.println("==============");
        Properties p11 = new Properties();
        //将文件内容读取到集合中
        p11.load(new FileReader("F:\\生活\\a.txt"));
        //对读取到的键值对集合进行遍历
        Set<String> set = p11.stringPropertyNames();
        String value=null;
        for (String key : set) {
            value=p11.getProperty(key);
            System.out.println(key+","+value);
        }
        System.out.println(p11);
    }

文件内容

# mapData \u5B58\u5165\u6587\u4EF6
#Fri Jul 10 17:52:49 CST 2020
003=ccc
002=bbb
001=aaa

案例:游戏次数
java 对象序列化 properties IO流

'根据结果,修改记录'


package a1;

import a0.game;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;

//"F:\\生活\\a.txt"
public class PropertiesDemo03 {
    public static void main(String[] args) throws IOException {
        String path = "F:\\生活\\a.txt";
        Properties p = new Properties();
        p.load(new FileReader(path));
        int count = Integer.parseInt(p.getProperty("count"));

        System.out.println("是否重新玩游戏,Y:重新玩,其他继续");
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();

        game game = new game();

        if (line.equals("Y")) {
            count = 0;
            game.setNum(0);
        }

        if (count < 2 && count >= 0) {
            game.guess();
            count++;
            p.setProperty("count", String.valueOf(count));
            FileWriter fw = new FileWriter(path);
            p.store(fw, null);
            fw.close();
        } else {
            System.out.println("游戏试玩结束,想要继续玩,请充值(www.it.com)");
            return;
        }
    }
}

'猜游戏'

package a0;

import java.util.Random;
import java.util.Scanner;

public class game {
    private int num = 0;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    //按照标准类来做,
    //加上构造方法
    public game() {
    }

    //静态方法待调用
    public void guess() {
        Random rand = new Random(0L);
        int i = rand.nextInt(100) + 1;

        while (true) {
            while (true) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入你猜测的数字");
                int i1 = sc.nextInt();
                if (i1 > i) {
                    ++num;
                    System.out.println("你猜的大了,再猜");
                } else {
                    if (i1 >= i) {
                        System.out.println("恭喜你猜中了");
                        System.out.println("使用了" + num + "次");
                        return;
                    }
                    ++num;
                    System.out.println("你猜测的小了,再猜");
                }
            }
        }
    }
}