|
Java > IO > DeSerialize object from file |
|
DeSerialize object from file
The following example shows how to deserialize list of strings from a flat file.
|
package com.bethecoder.tutorials.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
public class ObjectInputStreamTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
List<String> list = null;
ObjectInputStream input =
new ObjectInputStream(new FileInputStream("C:\\example.ser"));
list = (List<String>) input.readObject();
input.close();
System.out.println("list : " + list);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
|
| |
It gives the following output,
list : [ONE, TWO, THREE, FOUR, FIVE, SIX]
|
|