???????Java?????л??????л?
???????????? ???????[ 2016/2/29 11:50:30 ] ??????????????? ???????????
???????л????????????????Ρ????????????紫??RMI??????С????????????ArrayList?????л???????Java???л???????????????漰?????????????
??????????Java?????л?
????????????java.io.Serializable??????????л?
????transient??????????
???????????????л?????
?????????????л?????????α??????
????ArrayList?????л?????????????
????Java????????л?
????Java????????????????д?????????Java?????????????£???е?JVM?????????????Щ??????????????????Щ?????????????????JVM??????????????????????????У??????????JVM????????????????(????)???????????????????????????????Java???????л???????????????ù????
???????Java???????л???????????????????????????????????δ?????????Щ?????????????????????????????л????????????”??”??????????????????????????????л??????????е?????????
??????????????????????????????л?????????RMI(??????????)???????????д???????????????????????л???Java???л?API???????????л??????????????????API?????á?
??????ζ?Java??????????л??????л?
??????Java?У?????????????java.io.Serializable?????????????????л?????????????δ???
????code 1 ???????User?????????л????????л?
package com.hollis;
import java.io.Serializable;
import java.util.Date;
/**
* Created by hollis on 16/2/2.
*/
public class User implements Serializable{
private String name;
private int age;
private Date birthday;
private transient String gender;
private static final long serialVersionUID = -6849794470754667710L;
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
"?? age=" + age +
"?? gender=" + gender +
"?? birthday=" + birthday +
'}';
}
}
????code 2 ??User???????л????????л???Demo
package com.hollis;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.util.Date;
/**
* Created by hollis on 16/2/2.
*/
public class SerializableDemo {
public static void main(String[] args) {
//Initializes The Object
User user = new User();
user.setName("hollis");
user.setGender("male");
user.setAge(23);
user.setBirthday(new Date());
System.out.println(user);
//Write Obj to File
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("tempFile"));
oos.writeObject(user);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(oos);
}
//Read Obj from File
File file = new File("tempFile");
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(file));
User newUser = (User) ois.readObject();
System.out.println(newUser);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(ois);
try {
FileUtils.forceDelete(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//output
//User{name='hollis'?? age=23?? gender=male?? birthday=Tue Feb 02 17:37:38 CST 2016}
//User{name='hollis'?? age=23?? gender=null?? birthday=Tue Feb 02 17:37:38 CST 2016}
???????л????????л??????
????1????Java?У?????????????java.io.Serializable?????????????????л???
????2?????ObjectOutputStream??ObjectInputStream???????????л????????л?
????3??????????????????л??????????????·???????????????£???????????????????????????л? ID ?????£??? private static final long serialVersionUID??
????4?????л????????澲???????
????5????????????????л???????????????Serializable ????
????6??Transient ???????????????????????л?????????????????ù??????????????????????л???????У?????????л???transient ???????????????????? int ????? 0??????????? null??
????7??????????????????????л???????????????????Щ????????е????????????????????????????????????л???????м????????????????н????????????????????з????л????????????????ж???????????????????????л??????????????
????ArrayList?????л?
?????????ArrayList???л????????????????????
????????????????л???????л?????
???????????????????????java.util.ArrayList?????
????code 3
????public class ArrayList<E> extends AbstractList<E>
????implements List<E>?? RandomAccess?? Cloneable?? java.io.Serializable
????{
????private static final long serialVersionUID = 8683452581122892189L;
????transient Object[] elementData; // non-private to simplify nested class access
????private int size;
????}
?????????????????????????????????????п??????ArrayList?????java.io.Serializable????????????????????????л????????л??????elementData??transient???????????????????????????????л?????????????????д???Demo?????????????????
????code 4
????public static void main(String[] args) throws IOException?? ClassNotFoundException {
????List<String> stringList = new ArrayList<String>();
????stringList.add("hello");
????stringList.add("world");
????stringList.add("hollis");
????stringList.add("chuang");
????System.out.println("init StringList" + stringList);
????ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("stringlist"));
????objectOutputStream.writeObject(stringList);
????IOUtils.close(objectOutputStream);
????File file = new File("stringlist");
????ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
????List<String> newStringList = (List<String>)objectInputStream.readObject();
????IOUtils.close(objectInputStream);
????if(file.exists()){
????file.delete();
????}
????System.out.println("new StringList" + newStringList);
????}
????//init StringList[hello?? world?? hollis?? chuang]
????//new StringList[hello?? world?? hollis?? chuang]
???????ArrayList??????????ArrayList????????????????????????elementData??????????????б??е?????????????????????????????????????????????л??????????????????code 4???????????л???????л???List?е????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11