1. ??? ? ????
Serialization : ??? ?????. ?? ????? ??? ??? ????? ?? ???? ??? ????(???? ??) ? ???? Java ?? ??????. ? ????? ????? ?? ????? ??? ??? ??? ????? ???? ????? ?????.
即將對象轉(zhuǎn)化為二進制,用于保存,或者網(wǎng)絡(luò)傳輸。
????: ??? ??? ?? ??? ?? ???? ??? ?? ???? Java ?? ??? ???? ???????. ?, ??? ???? ??? ???? ???? ??? ??? ??? ???? ??
與序列化相反,將二進制轉(zhuǎn)化成對象。
2. ???? ??
#?? ??#① ???? ?? ??? ???? ??????? ???? ?? ? ② ??? ???? ????? ?? ??? ???? ?? ?; RMI? ?? ??? ???? ??一些應(yīng)用場景,涉及到將對象轉(zhuǎn)化成二進制,序列化保證了能夠成功讀取到保存的對象。
3.Java ??? ??
?? ???? ????? ?? ???? ??? ???? ????. ??? ?? ?????
IO ???? ?? ???? ???? ??? ??? ???? ??? ??? ??? ?? ?? ? ????. ?? ??? ???? ??? ?? ?????? ?????:import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = 1L; private String name; private int 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 "User [name=" + name + ", age=" + age + "]"; } }?? ???? ???? ??? ???? ?? ?? ?? ???? ?????:
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializeUtil { // 保存對象,序列化 public static void saveObject(Object object) throws Exception { ObjectOutputStream out = null; FileOutputStream fout = null; try { fout = new FileOutputStream("D:/1.txt"); out = new ObjectOutputStream(fout); out.writeObject(object); } finally { fout.close(); out.close(); } } // 讀取對象,反序列化 public static Object readObject() throws Exception { ObjectInputStream in = null; FileInputStream fin = null; try { fin = new FileInputStream("D:/1.txt"); in = new ObjectInputStream(fin); Object object = in.readObject(); return object; } finally { fin.close(); in.close(); } } }#???? # ???:
public class Main { public static void main(String[] args) { User user = new User(); user.setName("旭旭寶寶"); user.setAge(33); // 保存 try { SerializeUtil.saveObject(user); } catch (Exception e) { System.out.println("保存時異常:" + e.getMessage()); } // 讀取 User userObject; try { userObject = (User) SerializeUtil.readObject(); System.out.println(userObject); } catch (Exception e) { System.out.println("讀取時異常:" + e.getMessage()); } } }
??? ??:
??? ??? ??? ????? ??? ?? ?? ?????. ?? ??? ?????? ???? ??? ??? ?????. Serialiable ????? ??? ??? ?????:public class User { private String name; private int 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 "User [name=" + name + ", age=" + age + "]"; } }

????? ???? ? serialVersionUID ??? ?????. ??? IDprivate static final long serialVersionUID = 1L;
? ??? ID? ???? ?? ??? ???? ?? ??? ???! Java? ??? ????? ??? ???? serialVersionUID? ???? ?? ???? ?????. JVM? ?? ??? ???? serialVersionUID? ?? ??? ???? serialVersionUID? ?????. ??? ??? ???? ?? ???? ??? ??? ?????.
即序列化ID是為了保證成功進行反序列化5. ?? ??? ID
"serialVersionUID"?? ??? ???? ????? ???? ?? ??, long ??? ??? ?? ?? Java ??? ????? ??? ?? ??? ???? ???? ???? serialVersionUID? ???? ?????. ? ?? ??? ??? ???? ??? serialVersionUID? ?????. ?? ?? ???? ??? ? ??? ??? ?? ?? ?? ???? ?? ?? ???? ?? ??? ???? ???. ?? deserialization ?? serialVersionUID? ???? ?? deserialization? ???? ???. ???? ??? ???? ???? ?? ???? "serialVersionUID" ??? ????? ?? ?? ???? ?? ??? ? ????? ??? ? ????. 如果沒有顯示指定serialVersionUID,會自動生成一個。
只有同一次編譯生成的class才會生成相同的serialVersionUID。
但是如果出現(xiàn)需求變動,Bean類發(fā)生改變,則會導(dǎo)致反序列化失敗。為了不出現(xiàn)這類的問題,所以我們最好還是顯式的指定一個
serialVersionUID。
1 ?? ??? ????? ????(??, ???) #?? ??#2. ?? ???? ???? ???? ?? ???? ??? ?? ?????? ????? ???? ??? ???? ???? ?????.
3. ??? ???? ??? ?? ??? ???? ?? ?? ??? ????? ???? ??? ??????.子類序列化時: 如果父類沒有實現(xiàn)Serializable接口,沒有提供默認構(gòu)造函數(shù),那么子類的序列化會出錯; 如果父類沒有實現(xiàn)Serializable接口,提供了默認的構(gòu)造函數(shù),那么子類可以序列化,父類的成員變量不會被序列化。如果父類 實現(xiàn)了Serializable接口,則父類和子類都可以序列化。
7. ?? ???? ??? ????? ??—Protostuff
?? Java? ?? ??? ??? ??? ??? ?????)? ?? ????? ????. github?? ??? ???? ???? ????? ????: https://github.com/eihay/jvm-serializers/wiki
?? ??? ?? ?? Google?? ??? Colfer???. ??? Colfer? ????? ?? ??? ??? ??? Protostuff ??? ?????? ?????. ?????? ????? ? ?? ?????(?? ? ???)? ????? ???.
①github ??: https://github.com/protostuff/protostuff
<dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-core</artifactId> <version>1.5.9</version> </dependency>
<dependency> <groupId>io.protostuff</groupId> <artifactId>protostuff-core</artifactId> <version>1.5.9</version> </dependency>?? ?? ??
import com.dyuproject.protostuff.LinkedBuffer; import com.dyuproject.protostuff.ProtobufIOUtil; import com.dyuproject.protostuff.ProtostuffIOUtil; import com.dyuproject.protostuff.Schema; import com.dyuproject.protostuff.runtime.RuntimeSchema; public class Main { public static void main(String[] args) { User user = new User(); user.setName("旭旭寶寶"); user.setAge(33); Schema<User> schema = RuntimeSchema.getSchema(User.class); // 保存對象,序列化,轉(zhuǎn)化二進制數(shù)據(jù) LinkedBuffer buffer = LinkedBuffer.allocate(512); final byte[] protostuff; try { protostuff = ProtobufIOUtil.toByteArray(user, schema, buffer); } finally { buffer.clear(); } // 讀取對象,反序列化 User userObject = schema.newMessage(); ProtostuffIOUtil.mergeFrom(protostuff, userObject, schema); System.out.println(userObject); } }??? ???? ??? ?? ?????? ???? ????
public class User { private String name; private int 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 "User [name=" + name + ", age=" + age + "]"; } }??? ??:
若要要整合Redis使用,也可以寫成一個工具類:
import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import com.dyuproject.protostuff.LinkedBuffer; import com.dyuproject.protostuff.ProtobufIOUtil; import com.dyuproject.protostuff.Schema; import com.dyuproject.protostuff.runtime.RuntimeSchema; public class SerializeUtil { private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<>(); @SuppressWarnings("unchecked") public static <T> byte[] serializer(T obj) { Class<T> clazz = (Class<T>) obj.getClass(); Schema<T> schema = getSchema(clazz); return ProtobufIOUtil.toByteArray(obj, schema, LinkedBuffer.allocate(256)); } public static <T> T deSerializer(byte[] bytes, Class<T> clazz) { T message; try { message = clazz.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } Schema<T> schema = getSchema(clazz); ProtobufIOUtil.mergeFrom(bytes, message, schema); return message; } @SuppressWarnings("unchecked") public static <T> Schema<T> getSchema(Class<T> clazz) { Schema<T> schema = (Schema<T>) cachedSchema.get(clazz); if (schema == null) { schema = RuntimeSchema.createFrom(clazz); if (schema != null) { cachedSchema.put(clazz, schema); } } return schema; } }
這樣即使我們的User類就不用再實現(xiàn)Serialiable接口了,同樣可以進行序列化,效率也更高。
php中文網(wǎng),大量的免費Java入門教程,歡迎在線學習!
? ??? ???? ????? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











Java????? ?? ??? ? ?? ???? ??? ????. 1. Entryset? ???? ?? ?? ??? ?????. ?? ???? ????? ?????. 2. ?? ?? ?? ???? ?? ? ?? ?? ?? ?????. 3. ?? ??? ?????? Java8? foreach? ??????. Entryset? ?? ? ? ?? ?? ? ?? ??? ???? ? ??? ?? ?????. ? ??? ?? ?? ?? ?????? ?????. ?? ? ? ??? ?? ?? Keyset () ?? values ()? ????? ?? ?? ?? Map.Get (?)? ?? ?? ?? ? ????. Java 8? foreach ((?, ?)-& gt? ??? ? ????

Java??? ?? ???? ?? ?? ??? ????? ???? ? ???? ???? ?? ?? ??? ??? ???? ? ?????. 1. ??? ??? ?? ? ????????. ?? () ???? ?? ???? ?? ??? ?????. ??? ?? ??? ?? ???? ?? ????? ???? ?? ????? ???? ?????. 2. ???? ?? ?? ? ?? ???????, ??? ???? ?? ?? ??? ??? ??? ??? Compare () ???? ?? ????, ??? ?? ??? ??? ? ??? ?? ??? ?? ?????. ?? ???? ??? ??? ?? ?? ? ?? ? ? ??? ??? ??? ????? ??? ??? ??? ????.

Java? ?? ??? ??? ????? ?? ? ???? ???? ???? ???? ???? ????. 1. ???? ?? ?? ??? ?? ???? ???? InputStreamReader ? OutputStreamWriter? ???? ??? ?? ???? ???? ??? ?? ? ?? ??? ??????. 2. ???? ???? ???? ?? ? ? ?? ?? ??????? ??? ??? ?? ??? ???? ?????? ???? ?? ??? ??????. 3. String.getBytes () ? Newstring (byte [])????? ???? ??? ??? ?? ??? ??? ??? ?? ?? Standardcharsets.utf_8? ???? ??????. ???,

JavaScript ??? ??? ?? ?? ? ?? ???? ????. ?? ???? ???, ??, ??, ?, ???? ?? ? ??? ?????. ?? ????? ?? ?? ? ? ??? ????? ?? ??? ??? ????. ??, ?? ? ??? ?? ?? ??? ??? ??? ???? ??? ??? ???? ??? ?? ??? ????. ?? ? ????? ??? ???? ? ??? ? ??? TypeofNull? ??? ?????? ??? ? ????. ? ? ?? ??? ???? ?????? ????? ???? ??? ???? ? ??? ? ? ????.

injava, thestatickeywordmeansamembeLongstotheclassitself, nottoinstances.StaticvariablesAresharedAcrossAllInstances ? OutObjectCreation, ??? ForgloBalTrackingorConstants.StaticMethodsOperateateAteAteClassEvel, canceCcessnon-StaticMbers, statice

STD :: Chrono? ?? ?? ??, ?? ?? ??, ?? ?? ? ?? ?? ? ?? ?? ??? ???? C?? ???? ??? ?????. 1. std :: chrono :: system_clock :: now ()? ???? ?? ??? ?? ? ??? ?? ??? ???? ?? ? ? ??? ??? ??? ???? ?? ?? ? ????. 2. std :: Chrono :: steady_clock? ???? ?? ??? ???? ?? ??? ???? duration_cast? ?? ?? ?, ? ? ?? ??? ??????. 3. ?? (time_point) ? ?? (??)? ?? ??? ? ? ??? ?? ??? ? ?? epoch (epoch)???? ???????.

?? ?? Java? ?? ???? ?? ? ? ? ????? ????, ? ??? ??? ??? ??? ???? ? ????. 1. ?? ?? hashcode () ???? ???? ?? ?? ???? ?? ??? ?? ?? ???? ?????. 2. ?? ??? ??? ?? ?? ???? ??? ??? ? ????. ?? ??? ?? ? ??? ??? ?????. JDK8 ? ?? ? ??? ?? ?? (?? ?? 8) ??? ????? ?? ???? ?? ? ??? ?????. 3. ??? ?? ???? ?? ???? ?? equals () ? hashcode () ???? ?? ???????. 4. ?? ?? ??? ???? ?????. ?? ?? ??? ???? ?? ?? (?? 0.75)? ??? ?? ? ???; 5. ?? ?? ??? ??? ??? Multithreaded?? Concu? ???????.

ReintrantLock? ??? ? ??? Java??? ??? ??? ??? ?????. 1. ? ??? ?? ?? ?? (trylock ()), ?? ?? (TryLock (Longtimeout, TimeUnitunit)) ? ???? ?? ?? ????? ?? ??? ?????. 2. ??? ???? ? ???? ?? ? ????. 3.?? ??? ??/?? ????? ???? ?? ?? ?? ??? ?????. 4. ?? ?? ??? ???? ??????, ?? ??? ??? ?? ?? ???? ?? ?? ()? ???????. 5. ??? ?? ??? ?? ?? ??? ?? ??? ?? ?? ??? ??? ??? ????? ????? ??? ?? ?? ?? ???? ??? Synchro? ?????.
