mirror of
https://github.com/Estom/notes.git
synced 2026-02-04 11:04:21 +08:00
Java内容重新整理删除过期的东西
This commit is contained in:
74
Java/JavaDemo/codedemo/java/lang/serialization/Man.java
Normal file
74
Java/JavaDemo/codedemo/java/lang/serialization/Man.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package cn.aofeng.demo.java.lang.serialization;
|
||||
|
||||
import java.io.Externalizable;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInput;
|
||||
import java.io.ObjectOutput;
|
||||
|
||||
/**
|
||||
* 自定义序列化和反序列化。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class Man implements Externalizable {
|
||||
|
||||
private String manName;
|
||||
|
||||
private int manAge;
|
||||
|
||||
private transient String password;
|
||||
|
||||
public Man() {
|
||||
// nothing
|
||||
}
|
||||
|
||||
public Man(String name, int age) {
|
||||
this.manName = name;
|
||||
this.manAge = age;
|
||||
}
|
||||
|
||||
public Man(String name, int age, String password) {
|
||||
this.manName = name;
|
||||
this.manAge = age;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getManName() {
|
||||
return manName;
|
||||
}
|
||||
|
||||
public void setManName(String manName) {
|
||||
this.manName = manName;
|
||||
}
|
||||
|
||||
public int getManAge() {
|
||||
return manAge;
|
||||
}
|
||||
|
||||
public void setManAge(int manAge) {
|
||||
this.manAge = manAge;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeExternal(ObjectOutput out) throws IOException {
|
||||
out.writeObject(manName);
|
||||
out.writeInt(manAge);
|
||||
out.writeObject(password);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
|
||||
manName = (String) in.readObject();
|
||||
manAge = in.readInt();
|
||||
password = (String) in.readObject();
|
||||
}
|
||||
|
||||
}
|
||||
79
Java/JavaDemo/codedemo/java/lang/serialization/People.java
Normal file
79
Java/JavaDemo/codedemo/java/lang/serialization/People.java
Normal file
@@ -0,0 +1,79 @@
|
||||
package cn.aofeng.demo.java.lang.serialization;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 默认序列化和
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class People implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 6235620243018494633L;
|
||||
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
|
||||
private transient String address;
|
||||
|
||||
private static String sTestNormal;
|
||||
|
||||
private static transient String sTestTransient;
|
||||
|
||||
public People(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public People(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public People(String name, int age, String address) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getsTestNormal() {
|
||||
return sTestNormal;
|
||||
}
|
||||
|
||||
public void setsTestNormal(String sTestNormal) {
|
||||
People.sTestNormal = sTestNormal;
|
||||
}
|
||||
|
||||
public String getsTestTransient() {
|
||||
return sTestTransient;
|
||||
}
|
||||
|
||||
public void setsTestTransient(String sTestTransient) {
|
||||
People.sTestTransient = sTestTransient;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package cn.aofeng.demo.java.lang.serialization;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 关键字 transient 测试。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class TransientDemo {
|
||||
|
||||
private static Logger _logger = LoggerFactory.getLogger(TransientDemo.class);
|
||||
|
||||
private String _tempFileName = "TransientDemo";
|
||||
|
||||
/**
|
||||
* 将对象序列化并保存到文件。
|
||||
*
|
||||
* @param obj 待序列化的对象
|
||||
*/
|
||||
public void save(Object obj) {
|
||||
ObjectOutputStream outs = null;
|
||||
try {
|
||||
outs = new ObjectOutputStream(
|
||||
new FileOutputStream( getTempFile(_tempFileName) ));
|
||||
outs.writeObject(obj);
|
||||
} catch (IOException e) {
|
||||
_logger.error("save object to file occurs error", e);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(outs);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件读取内容并反序列化成对象。
|
||||
*
|
||||
* @return {@link People}对象。如果读取文件出错 或 对象类型转换失败,返回null。
|
||||
*/
|
||||
public <T> T load() {
|
||||
ObjectInputStream ins = null;
|
||||
try {
|
||||
ins = new ObjectInputStream(
|
||||
new FileInputStream( getTempFile(_tempFileName)) );
|
||||
return ((T) ins.readObject());
|
||||
} catch (IOException e) {
|
||||
_logger.error("load object from file occurs error", e);
|
||||
} catch (ClassNotFoundException e) {
|
||||
_logger.error("load object from file occurs error", e);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(ins);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private File getTempFile(String filename) {
|
||||
return new File(getTempDir(), filename);
|
||||
}
|
||||
|
||||
private String getTempDir() {
|
||||
return System.getProperty("java.io.tmpdir");
|
||||
}
|
||||
|
||||
private void displayPeople(People people) {
|
||||
if (null == people) {
|
||||
return;
|
||||
}
|
||||
String template = "People[name:%s, age:%d, address:%s, sTestNormal:%s, sTestTransient:%s]";
|
||||
System.out.println( String.format(template, people.getName(), people.getAge(),
|
||||
people.getAddress(), people.getsTestNormal(), people.getsTestTransient()));
|
||||
}
|
||||
|
||||
private void displayMan(Man man) {
|
||||
if (null == man) {
|
||||
return;
|
||||
}
|
||||
String template = "Man[manName:%s, manAge:%d, password:%s]";
|
||||
System.out.println( String.format(template, man.getManName(), man.getManAge(), man.getPassword()) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
System.out.println(">>> Serializable测试");
|
||||
TransientDemo demo = new TransientDemo();
|
||||
|
||||
People people = new People("张三", 30, "中国广州");
|
||||
people.setsTestNormal("normal-first");
|
||||
people.setsTestTransient("transient-first");
|
||||
System.out.println("序列化之前的对象信息:");
|
||||
demo.displayPeople(people);
|
||||
demo.save(people);
|
||||
|
||||
// 修改静态变量的值
|
||||
people.setsTestNormal("normal-second");
|
||||
people.setsTestTransient("transient-second");
|
||||
|
||||
People fromLoad = demo.load();
|
||||
System.out.println("反序列化之后的对象信息:");
|
||||
demo.displayPeople(fromLoad);
|
||||
|
||||
|
||||
|
||||
System.out.println("");
|
||||
System.out.println(">>> Externalizable测试");
|
||||
Man man = new Man("李四", 10, "假密码");
|
||||
System.out.println("序列化之前的对象信息:");
|
||||
demo.displayMan(man);
|
||||
demo.save(man);
|
||||
|
||||
Man manFromLoadMan = demo.load();
|
||||
System.out.println("反序列化之后的对象信息:");
|
||||
demo.displayMan(manFromLoadMan);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user