1. ??? ? ????
Serialization : ??? ?????. ?? ????? ??? ??? ????? ?? ???? ??? ????(???? ??) ? ???? Java ?? ??????. ? ????? ????? ?? ????? ??? ??? ??? ????? ???? ????? ?????.
即將對(duì)象轉(zhuǎn)化為二進(jìn)制,用于保存,或者網(wǎng)絡(luò)傳輸。
????: ??? ??? ?? ??? ?? ???? ??? ?? ???? Java ?? ??? ???? ???????. ?, ??? ???? ??? ???? ???? ??? ??? ??? ???? ??
與序列化相反,將二進(jìn)制轉(zhuǎn)化成對(duì)象。
2. ???? ??
#?? ??#① ???? ?? ??? ???? ??????? ???? ?? ? ② ??? ???? ????? ?? ??? ???? ?? ?; RMI? ?? ??? ???? ??一些應(yīng)用場(chǎng)景,涉及到將對(duì)象轉(zhuǎn)化成二進(jìn)制,序列化保證了能夠成功讀取到保存的對(duì)象。
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 { // 保存對(duì)象,序列化 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(); } } // 讀取對(duì)象,反序列化 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("保存時(shí)異常:" + e.getMessage()); } // 讀取 User userObject; try { userObject = (User) SerializeUtil.readObject(); System.out.println(userObject); } catch (Exception e) { System.out.println("讀取時(shí)異常:" + 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是為了保證成功進(jìn)行反序列化5. ?? ??? ID
"serialVersionUID"?? ??? ???? ????? ???? ?? ??, long ??? ??? ?? ?? Java ??? ????? ??? ?? ??? ???? ???? ???? serialVersionUID? ???? ?????. ? ?? ??? ??? ???? ??? serialVersionUID? ?????. ?? ?? ???? ??? ? ??? ??? ?? ?? ?? ???? ?? ?? ???? ?? ??? ???? ???. ?? deserialization ?? serialVersionUID? ???? ?? deserialization? ???? ???. ???? ??? ???? ???? ?? ???? "serialVersionUID" ??? ????? ?? ?? ???? ?? ??? ? ????? ??? ? ????. 如果沒有顯示指定serialVersionUID,會(huì)自動(dòng)生成一個(gè)。
只有同一次編譯生成的class才會(huì)生成相同的serialVersionUID。
但是如果出現(xiàn)需求變動(dòng),Bean類發(fā)生改變,則會(huì)導(dǎo)致反序列化失敗。為了不出現(xiàn)這類的問題,所以我們最好還是顯式的指定一個(gè)
serialVersionUID。
1 ?? ??? ????? ????(??, ???) #?? ??#2. ?? ???? ???? ???? ?? ???? ??? ?? ?????? ????? ???? ??? ???? ???? ?????.
3. ??? ???? ??? ?? ??? ???? ?? ?? ??? ????? ???? ??? ??????.子類序列化時(shí): 如果父類沒有實(shí)現(xiàn)Serializable接口,沒有提供默認(rèn)構(gòu)造函數(shù),那么子類的序列化會(huì)出錯(cuò); 如果父類沒有實(shí)現(xiàn)Serializable接口,提供了默認(rèn)的構(gòu)造函數(shù),那么子類可以序列化,父類的成員變量不會(huì)被序列化。如果父類 實(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); // 保存對(duì)象,序列化,轉(zhuǎn)化二進(jìn)制數(shù)據(jù) LinkedBuffer buffer = LinkedBuffer.allocate(512); final byte[] protostuff; try { protostuff = ProtobufIOUtil.toByteArray(user, schema, buffer); } finally { buffer.clear(); } // 讀取對(duì)象,反序列化 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使用,也可以寫成一個(gè)工具類:
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類就不用再實(shí)現(xiàn)Serialiable接口了,同樣可以進(jìn)行序列化,效率也更高。
php中文網(wǎng),大量的免費(fèi)Java入門教程,歡迎在線學(xué)習(xí)!
? ??? ???? ????? ??? ?? ?????. ??? ??? 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)

settings.json ??? ??? ?? ?? ?? ?? ?? ??? ??? VSCODE ??? ??? ???? ? ?????. 1. ??? ?? ?? : Windows? C : \ Users \\ AppData \ Roaming \ Code \ User \ Settings.json, MacOS IS /users//library/applicationsupport/code/user/settings.json, linux? /home//.config/code/user/settings.json; 2. Workspace ?? ?? : .vscode/settings project root ????

JDBC ????? ???? ????? ?? ?? ?? ??? ?? ?? ??? ?? ? ?? ??? ?? ?? ?? ??? ???????. 1. ????? ????? Conn.SetAutoCommit (False)?? ??????. 2. ??? ? ????? ?? ?? SQL ??? ?????. 3. ?? ??? ??? ?? Conn.commit ()?? ???? ??? ???? ???? ?? ??? ???? Conn.Rollback ()?? ??????. ???, ? ??? ???? ????, ??? ???? ????, ?? ??? ??? ?? ??? ??? ???? ? ???????. ?? ?? ?? ???? ????? ??? ???? ?? ?? ???? ???? ??? ????? ?? ??? ??? ? ?? ???? ?? ????.

??? (DI) ISADESIGNPATTORNWHEREWHEDROUDIVESTESTESTETESTERGROWCONSTRUCTOR, 2.SPRINGFRAMEWWERTHUSENONTATIONS? ??@autowiredWithjava ?? CONCUTTATIONS LIKERWITHCONSTRUCTOR, ORFIELDINGESS.2.SPRINGFRAMEWWERTHUSENNOTATIONS

itertools.combinations? ?? ??? ???? ??? ?? ??? ???? ?? ? ?? ?? (?? ???)? ???? ? ?????. ???? ??? ????. 1. ?? ??? ??? ?? ( 'a', 'b'), ( 'a', 'c') ? ???? 2 ?? ?? ??? ?????. 2. ?? ??? ??? "ABC"? "ABD"? ?? ???? 3 ? ??? ???; 3. ? ??? ?? 1 5 = 6? ?? ?? ?? ??? ??? ?????. ?? ?? ??? ???????. ??? ??? ??? ??? ???? ??????, ??? AB? BA? ???? ???? ??, ??? ?? ??? ????.

???? ?? ?? ?? ?? ???? ?? ???? ???? ? ???? ?????. 1. @pytest.fixture ?????? ???? ???? ??????. 2. ??? ???? ???? ??? ???? ??; 3. ?? ?? ??? ?? ? ?? ??; 4. ??, ?? ?? ?? ?? ?? ??? ?? ?? ??; 5. ?? ???? Conftest.py? ???? ??? ?? ??? ???? ???? ?? ?? ? ??? ?? ??????.

java.lang.outofMemoryError : javaheapspace? ? ???? ???? ? ??, ??? ?? ? ? ??? ??? ???? ? ?? ?? ??? ?? ??? ??? ????????. 2. Metaspace ??? ??? ??? ?? ???? ?? ?? ??? ?? ?? ? ???? ????? MaxMetaspacesize? ???? ?????? ????????. 3. UnableTocreatenewnativeThread ?? ??? ??? ???? ?? ??? ?? ???? ??? ?? ???? ?? ??? ???????. 4. GCOVERHEADLIMITEXEDED? GC? ????? ???? ??? GC ??? ???? ????? ?? ?????.

?? ?? ? ?? ???? ???? ?? Java.Time ???? ???? ??????. 2. LocalDate, LocalDateTime ? LocalTime? ?? ?? ??? ??? ?????. 3. () ???? ???? ?? ??? ??? ????. 4. ???/???? ??? ???? ??? ????? ??? ??????. 5. ZonedDateTime ? Zoneid? ???? ???? ??????. 6. DateTimeFormatter? ?? ?? ? ?? ?? ?? ???; 7. ??? ?? ?? ?? ??? ????? ?? ??????. ?? Java? ?? ??? ???? ??? ??? ???? Java.Timeapi ??? ?? ??? ???????.

thejvmenablesjava? "WriteOnce, Runynywhere"??? ?? excodecodethroughfourmaincomponents : 1. theclassloadersubsystemloads, ??, ? intinitializes.classfilesusingbootsprap, extension, andapplicationclassloaders, ensuringsecureandlazyclasloa
