Java에서 객체를 문자열로 직렬화
Sheeraz Gul
2023년10월12일
이 튜토리얼은 Java에서 객체를 문자열로 직렬화하는 방법을 보여줍니다.
Java에서 객체를 문자열로 직렬화
객체를 문자열로 직렬화하기 위해 기본 64 인코딩을 사용할 수 있습니다. 두 개의 클래스를 생성하여 직렬화를 구현할 수 있습니다. 한 클래스는 Serializable
클래스를 구현하고 다른 클래스는 Serializable
클래스의 개체를 만들고 직렬화하는 데 사용됩니다.
Java 예제를 참조하십시오.
package delftstack;
import java.io.*;
import java.util.*;
public class Serialize_Object {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String Serialized_String = To_String(new Demo_Serialize());
System.out.println(" The Serialized String ");
System.out.println(Serialized_String);
Demo_Serialize Original_object = (Demo_Serialize) From_String(Serialized_String);
System.out.println("\n\nThe Original String");
System.out.println(Original_object);
}
private static Object From_String(String s) throws IOException, ClassNotFoundException {
byte[] Byte_Data = Base64.getDecoder().decode(s);
ObjectInputStream Object_Input_Stream =
new ObjectInputStream(new ByteArrayInputStream(Byte_Data));
Object Demo_Object = Object_Input_Stream.readObject();
Object_Input_Stream.close();
return Demo_Object;
}
private static String To_String(Serializable Demo_Object) throws IOException {
ByteArrayOutputStream Byte_Array_Output_Stream = new ByteArrayOutputStream();
ObjectOutputStream Object_Output_Stream = new ObjectOutputStream(Byte_Array_Output_Stream);
Object_Output_Stream.writeObject(Demo_Object);
Object_Output_Stream.close();
return Base64.getEncoder().encodeToString(Byte_Array_Output_Stream.toByteArray());
}
}
class Demo_Serialize implements Serializable {
private final static long serialVersionUID = 1;
int i = Integer.MAX_VALUE;
String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Double d = new Double(-1.0);
public String toString() {
return "DelftStack is a resource for everyone interested in "
+ "programming, embedded software, and electronics. "
+ "It covers the programming languages like Python, "
+ "C/C++, C#, and so on in this website's first "
+ "development stage. Open-source hardware also falls "
+ "in the website's scope, like Arduino, Raspberry Pi, and BeagleBone.";
}
}
위의 코드는 Demo_Serialize
클래스의 개체를 만들고 해당 개체를 문자열로 직렬화합니다. 위 코드의 출력은 다음과 같습니다.
The Serialized String
rO0ABXNyABlkZWxmdHN0YWNrLkRlbW9fU2VyaWFsaXplAAAAAAAAAAECAANJAAFpTAABZHQAEkxqYXZhL2xhbmcvRG91YmxlO0wAAXN0ABJMamF2YS9sYW5nL1N0cmluZzt4cH////9zcgAQamF2YS5sYW5nLkRvdWJsZYCzwkopa/sEAgABRAAFdmFsdWV4cgAQamF2YS5sYW5nLk51bWJlcoaslR0LlOCLAgAAeHC/8AAAAAAAAHQAGkFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFla
The Original String
DelftStack is a resource for everyone interested in programming, embedded software, and electronics. It covers the programming languages like Python, C/C++, C#, and so on in this website's first development stage. Open-source hardware also falls in the website's scope, like Arduino, Raspberry Pi, and BeagleBone.
작가: Sheeraz Gul
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook