一、序列化和反序列化
对象实现序列化后,可以进行网络传输或存储磁盘的功能。本质是,序列化就是将对象变成byte[]形式;反序列化 就是将byte[] 变成对象。
对象要实现序列化,类一定要实现Serializable接口
ObjectInputStream和ObjectOutputStream
直接读写对象,前提是对象的类实现Serializable接口
public static void main(String[] args) {
//将一个Person对象存储到temp.txt中
Person p = new Person("tom",20);
try {
FileOutputStream fos = new FileOutputStream("D:\\Lession\\Java2113/temp.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(p);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//读取temp.txt中存储的对象
try {
FileInputStream fis = new FileInputStream("D:\\Lession\\Java2113/temp.txt");
ObjectInputStream bis = new ObjectInputStream(fis);
Person p = (Person)bis.readObject();
System.out.println(p.getName()+","+p.getAge());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
二、对象进行序列化和反序列化的封装
获得对象的byte[] 及 将byte[] 变成对象 ;封装两个方法
需要使用ByteArrayInputStream和ByteArrayOutputStream ,是对byte[]的读写的流
public class Test {
public static void main(String[] args) {
Person p = new Person("tom",22);
Person p2 = new Person("jack",21);
Person p3 = new Person("smith",20);
List<Person> list = new ArrayList<Person>();
list.add(p);
list.add(p2);
list.add(p3);
byte[] bytes = getBytes(list);
List<Person> ps = (List<Person>)getObject(bytes);
for(Person person:ps) {
System.out.println(person.getName()+","+person.getAge());
}
}
/**
* 将对象序列化成byte[]
* @param obj
* @return
*/
public static byte[] getBytes(Object obj) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
return baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 将byte[] 反序列化成对象
* @param bytes
* @return
*/
public static Object getObject(byte[] bytes) {
try {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
三、Properties工具类
用来读取Properties配置文件的一个工具类 ,Java中的配置文件包括了.properties和.xml形式的配置文件, properties配置文件编写和读取相对简单,是以key=value的格式编写的 ,但不能表示数据的结构化 ; xml形式的 配置文件编写和读取相对复制,表示数据的结构化
Properties继承于Hashtable ,Properties会将配置信息读取到Hashtable中,通过key获得value
编程实现读取配置文件
public class TestProperties {
public static void main(String[] args) {
Properties p = new Properties();
try {
//将配置文件内容读取到 Properties 的Hashtable中
FileInputStream fis = new
FileInputStream("D:\\MyWork\\MyWorkspaces\\eclipse0106\\pjt0211\\src\\test.properties");
p.load(fis);
System.out.println(p.getProperty("username"));
System.out.println(p.getProperty("password"));
//向Hashtable中设置内容
p.setProperty("sfz", "1001100");
p.store(new
FileOutputStream("D:\\MyWork\\MyWorkspaces\\eclipse0106\\pjt0211\\src\\test.properties"), "***");
System.out.println(p.getProperty("sfz"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//类路径 (classPath) .class的编译后的文件目录
ResourceBundle bundle = ResourceBundle.getBundle("test");
System.out.println(bundle.getString("username"));
}