Flujo de objetos
Función: Se utiliza para almacenar y leer objetos de tipo de datos básicos o procesar el flujo de objetos. Su poder es escribir objetos en java en la fuente de datos, y también puede restaurar los datos en la fuente de datos.
Serialización y deserialización.
El mecanismo de serialización del objeto:
Serialización: permite convertir objetos Java en la memoria en flujos binarios independientes de la plataforma, lo que permite que este flujo secundario persista en el disco o transmita este flujo binario a otro nodo de red a través de la red.
Deserialización: cuando otros programas adquieren esta secuencia secundaria, se pueden restaurar al objeto Java original.
public class ObjectInputStreamOutputStreamTest {
@Test
public void testObjectOutputStream ( ) {
ObjectOutputStream oos = new ObjectOutputStream ( new FileOutputStream ( "hello,dat" ) ) ;
oos. writeObject ( new String ( "我爱coding,coding爱我" ) ) ;
oos. flush ( ) ;
oos. close ( ) ;
}
@Test
public void testObjectInputStream ( ) {
ObjectInputStream ois = new ObjectInputStream ( new FileOutputStream ( "object.dat" ) ) ;
Object obj = ois. readObject ( ) ;
String str = ( String) obj;
System. out. println ( str) ;
ois. close ( ) ;
}
}
Para que un objeto Java sea serializable, se deben cumplir los siguientes requisitos
package com. ntt. sts;
import java. io. Serializable;
import java. util. ArrayList;
import java. util. List;
public class HomeWork implements Serializable {
private static final long serialVersionUID = - 12312412412412 L;
private String title;
private String content;
public String getTitle ( ) {
return title;
}
public void setTitle ( String title) {
this . title = title;
}
public String getContent ( ) {
return content;
}
public void setContent ( String content) {
this . content = content;
}
public HomeWork ( String title, String content) {
this . title = title;
this . content = content;
}
public HomeWork ( ) {
}
@Override
public String toString ( ) {
return "HomeWork{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
'}' ;
}
}
package com. ntt. sts;
import org. junit. Test;
import java. io. *;
public class ObjectOutputInputStreamTest {
@Test
public void testObjectOutputStream ( ) throws IOException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream ( new FileOutputStream ( "hello.txt" ) ) ;
objectOutputStream. writeObject ( new HomeWork ( "日语作业" , "123456" ) ) ;
objectOutputStream. flush ( ) ;
objectOutputStream. close ( ) ;
}
@Test
public void testObjectInputStream ( ) throws IOException{
ObjectInputStream objectInputStream = new ObjectInputStream ( new FileInputStream ( "hello.txt" ) ) ;
try {
Object o = objectInputStream. readObject ( ) ;
HomeWork homeWork = new HomeWork ( ) ;
homeWork = ( HomeWork) o;
System. out. println ( homeWork. toString ( ) ) ;
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
}
}
}
Se utiliza junto con flujos de entrada y salida.
package com. ntt. sts;
import org. junit. Test;
import java. io. *;
public class ObjectOutputInputStreamTest {
@Test
public void testObjectOutputStream ( ) throws IOException {
File file = new File ( "hello.txt" ) ;
FileReader fileReader = new FileReader ( file) ;
char [ ] chars = new char [ 1024 ] ;
int lenth;
String content= "" ;
while ( ( lenth = fileReader. read ( chars) ) != - 1 ) {
content = new String ( chars, 0 , lenth) ;
System. out. println ( content) ;
}
ObjectOutputStream objectOutputStream = new ObjectOutputStream ( new FileOutputStream ( "homeWork.txt" ) ) ;
objectOutputStream. writeObject ( new HomeWork ( content) ) ;
fileReader. close ( ) ;
objectOutputStream. flush ( ) ;
objectOutputStream. close ( ) ;
}
@Test
public void testObjectInputStream ( ) throws Exception{
ObjectInputStream objectInputStream = new ObjectInputStream ( new FileInputStream ( "homeWork.txt" ) ) ;
Object o = objectInputStream. readObject ( ) ;
HomeWork homeWork = new HomeWork ( ) ;
homeWork = ( HomeWork) o;
String content = homeWork. getContent ( ) ;
System. out. println ( homeWork. toString ( ) ) ;
File file = new File ( "homeWork1.txt" ) ;
FileWriter fileWriter = new FileWriter ( file) ;
fileWriter. write ( content) ;
fileWriter. close ( ) ;
objectInputStream. close ( ) ;
}
}
Condiciones necesarias para la serialización y deserialización
Implementar interfaz serializable
定义 private static final long serialVersionUID = -12312412412412L;
Tanto el objeto como los atributos del objeto deben ser serializables