文章目录
1 序列化流
1.1 序列化
* 序列化流:把对象按照流一样的方式存入文本文件或者在网络中传输(写)。对象 -- 流数据(ObjectOutputStream)
* 反序列化流:把文本文件中的流对象数据或者网络中的流对象数据还原成对象(读)。流数据 -- 对象(ObjectInputStream)
1.2 如何实现序列化
如何实现序列化呢?
让被序列化的对象所属类实现序列化接口。
该接口是一个标记接口。没有功能需要实现。
1.3 需要注意的问题
注意问题:
A:
把数据写到文件后,在去修改类会产生一个问题。如何解决该问题呢?
在类文件中,给出一个固定的序列化id值。(可以用ide帮我们生产的序列化id值)
B:
我一个类中可能有很多的成员变量,有些我不想进行序列化。请问该怎么办呢?
使用transient关键字声明不需要序列化的成员变量(这样变量的值不能被保存,只能使用默认的值0)
案例:
/*
* NotSerializableException:未序列化异常
*
* 类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
* 该接口居然没有任何方法,类似于这种没有方法的接口被称为标记接口。(使用Serializable标记接口表示你可以被序列化了)
*
* java.io.InvalidClassException:
* cn.itcast_07.Person; local class incompatible:
* stream classdesc serialVersionUID = -2071565876962058344,
* local class serialVersionUID = -8345153069362641443
*
* 为什么会有问题呢?
* Person类实现了序列化接口,那么它本身也应该有一个标记值。
* 这个标记值假设是100。
* 开始的时候:
* Person.class -- id=100
* wirte数据: oos.txt -- id=100
* read数据: oos.txt -- id=100
*
* 现在:
* Person.class -- id=200
* wirte数据: oos.txt -- id=100
* read数据: oos.txt -- id=100
* 我们在实际开发中,可能还需要使用以前写过的数据,不能重新写入。怎么办呢?
* 回想一下原因是因为它们的id值不匹配。
* 每次修改java文件的内容的时候,class文件的id值都会发生改变。
* 而读取文件的时候,会和class文件中的id值进行匹配。所以,就会出问题。
* 但是呢,如果我有办法,让这个id值在java文件中是一个固定的值,这样,你修改文件的时候,这个id值还会发生改变吗?
* 不会。现在的关键是我如何能够知道这个id值如何表示的呢?
* 用idea帮我们生成的id
*
* 我们要知道的是:
* 看到类实现了序列化接口的时候,要想解决黄色警告线问题,就可以自动产生一个序列化id值。
* 而且产生这个值以后,我们对类进行任何改动,它读取以前的数据是没有问题的。
*
* 注意:
* 我一个类中可能有很多的成员变量,有些我不想进行序列化。请问该怎么办呢?
* 使用transient关键字声明不需要序列化的成员变量(这样变量的值不能被保存,只能使用默认的值0)
*/
public class Person implements Serializable {
private static final long serialVersionUID = -2071565876962058344L;
private String name;
// private int age;
private transient int age;
// int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
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 [name=" + name + ", age=" + age + "]";
}
}
/**
测试类
*/
public class ObjectStreamDemo {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
// 由于我们要对对象进行序列化,所以我们先自定义一个类
// 序列化数据其实就是把对象写到文本文件
write();
read();
}
private static void read() throws IOException, ClassNotFoundException {
// 创建反序列化对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"oos.txt"));
// 还原对象
Object obj = ois.readObject();
// 释放资源
ois.close();
// 输出对象
System.out.println(obj);
}
private static void write() throws IOException {
// 创建序列化流对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"oos.txt"));
// 创建对象
Person p = new Person("林青霞", 27);
// public final void writeObject(Object obj)
oos.writeObject(p);
// 释放资源
oos.close();
}
}
2 Properties
2.1 Properties概述
* Properties:属性集合类。是一个可以和IO流相结合使用的集合类。
* Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
*
* 是Hashtable的子类,说明是一个Map集合。
public class PropertiesDemo {
public static void main(String[] args) {
// 作为Map集合的使用
// 下面这种用法是错误的,一定要看API,如果没有<>,就说明该类不是一个泛型类,在使用的时候就不能加泛型
// Properties<String, String> prop = new Properties<String, String>();
Properties prop = new Properties();
// 添加元素
prop.put("it002", "hello");
prop.put("it001", "world");
prop.put("it003", "java");
// System.out.println("prop:" + prop);
// 遍历集合
Set<Object> set = prop.keySet();
for (Object key : set) {
Object value = prop.get(key);
System.out.println(key + "---" + value);
}
}
}
2.2 特殊功能
* public Object setProperty(String key,String value):添加元素
* public String getProperty(String key):获取元素
* public Set<String> stringPropertyNames():获取所有的键的集合
public class PropertiesDemo2 {
public static void main(String[] args) {
// 创建集合对象
Properties prop = new Properties();
// 添加元素
prop.setProperty("张三", "30");
prop.setProperty("李四", "40");
prop.setProperty("王五", "50");
// public Set<String> stringPropertyNames():获取所有的键的集合
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key + "---" + value);
}
}
}
/*
* class Hashtalbe<K,V> { public V put(K key,V value) { ... } }
*
* class Properties extends Hashtable { public V setProperty(String key,String
* value) { return put(key,value); } }
*/
2.3 和IO流结合的方法
* 这里的集合必须是Properties集合:
* public void load(Reader reader):把文件中的数据读取到集合中
* public void store(Writer writer,String comments):把集合中的数据存储到文件,comments - 属性列表的描述
*
* 单机版游戏:
* 进度保存和加载。
* 三国群英传,三国志,仙剑奇侠传...
*
* 吕布=1
* 方天画戟=1
public class PropertiesDemo3 {
public static void main(String[] args) throws IOException {
// myLoad();
myStore();
}
private static void myStore() throws IOException {
// 创建集合对象
Properties prop = new Properties();
prop.setProperty("林青霞", "27");
prop.setProperty("武鑫", "30");
prop.setProperty("刘晓曲", "18");
//public void store(Writer writer,String comments):把集合中的数据存储到文件
Writer w = new FileWriter("name.txt");
prop.store(w, "helloworld");
w.close();
}
private static void myLoad() throws IOException {
Properties prop = new Properties();
// public void load(Reader reader):把文件中的数据读取到集合中
// 注意:这个文件的数据必须是键值对形式
Reader r = new FileReader("prop.txt");
prop.load(r);
r.close();
System.out.println("prop:" + prop);
}
}
案例1
/*
* 我有一个文本文件(user.txt),我知道数据是键值对形式的,但是不知道内容是什么。
* 请写一个程序判断是否有“lisi”这样的键存在,如果有就改变其实为”100”
*
* 分析:
* A:把文件中的数据加载到集合中
* B:遍历集合,获取得到每一个键
* C:判断键是否有为"lisi"的,如果有就修改其值为"100"
* D:把集合中的数据重新存储到文件中
*/
public class PropertiesTest {
public static void main(String[] args) throws IOException {
// 把文件中的数据加载到集合中
Properties prop = new Properties();
Reader r = new FileReader("user.txt");
prop.load(r);
r.close();
// 遍历集合,获取得到每一个键
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
// 判断键是否有为"lisi"的,如果有就修改其值为"100"
if ("lisi".equals(key)) {
prop.setProperty(key, "100");
break;
}
}
// 把集合中的数据重新存储到文件中
Writer w = new FileWriter("user.txt");
prop.store(w, null);//为null就是没有对列表的描述
w.close();
}
}
3 NIO(New IO)
* nio包在JDK4出现,提供了IO流的操作效率。
*
* JDK7的之后的nio:
* Path:路径
* Paths:有一个静态方法返回一个路径
* public static Path get(URI uri)
* Files:提供了静态方法供我们使用
* public static long copy(Path source,OutputStream out):复制文件
* public static Path write(Path path,Iterable<? extends CharSequence> lines,Charset cs,OpenOption... options):把集合的数据写到文件
public class NIODemo {
public static void main(String[] args) throws IOException {
// public static long copy(Path source,OutputStream out)
// Files.copy(Paths.get("ByteArrayStreamDemo.java"), new
// FileOutputStream(
// "Copy.java"));
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
Files.write(Paths.get("array.txt"), array, Charset.forName("GBK"));
}
}