JAVA 대상 & byte 배열 간 전환
8713 단어 JAVA 기초
package test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* JAVA &Byte
* @author liqqc
*/
public class Object2Array {
private static final Logger LOGGER = LoggerFactory.getLogger(Object2Array.class);
public static void main(String[] args) {
Person person = new Person("Jone", 25, "good person");
byte[] objectToByteArray = objectToByteArray(person);
System.err.println("objectToByteArray: " + objectToByteArray);
Object byteArrayToObject = byteArrayToObject(objectToByteArray);
System.err.println("byteArrayToObject: " + byteArrayToObject.toString());
}
/**
* Byte
* @param obj
* @return
*/
public static byte[] objectToByteArray(Object obj) {
byte[] bytes = null;
ByteArrayOutputStream byteArrayOutputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj);
objectOutputStream.flush();
bytes = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
LOGGER.error("objectToByteArray failed, " + e);
} finally {
if (objectOutputStream != null) {
try {
objectOutputStream.close();
} catch (IOException e) {
LOGGER.error("close objectOutputStream failed, " + e);
}
}
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
LOGGER.error("close byteArrayOutputStream failed, " + e);
}
}
}
return bytes;
}
/**
* Byte
* @param bytes
* @return
*/
public static Object byteArrayToObject(byte[] bytes) {
Object obj = null;
ByteArrayInputStream byteArrayInputStream = null;
ObjectInputStream objectInputStream = null;
try {
byteArrayInputStream = new ByteArrayInputStream(bytes);
objectInputStream = new ObjectInputStream(byteArrayInputStream);
obj = objectInputStream.readObject();
} catch (Exception e) {
LOGGER.error("byteArrayToObject failed, " + e);
} finally {
if (byteArrayInputStream != null) {
try {
byteArrayInputStream.close();
} catch (IOException e) {
LOGGER.error("close byteArrayInputStream failed, " + e);
}
}
if (objectInputStream != null) {
try {
objectInputStream.close();
} catch (IOException e) {
LOGGER.error("close objectInputStream failed, " + e);
}
}
}
return obj;
}
static class Person implements Serializable {
private static final long serialVersionUID = 3559533002594201715L;
private String name;
private Integer age;
private String desc;
public Person(String name, int age, String desc) {
this.name = name;
this.age = age;
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", desc=" + desc + "]";
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JAVA 의 XML 파일 -- 파일 쓰기Transformer 대상 의 방법 transform (Source, stream) 방법 은 우리 가 만 든 Document 대상 을 XML 파일 로 바 꿀 수 있 습 니 다. 이렇게 만 든 xml 파일 의 모든 줄...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.