Java by API/java.io/ObjectInputStream — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:32, 31 мая 2010
new ObjectInputStream(InputStream in)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] a) throws Exception {
List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
FileOutputStream fos = new FileOutputStream("list.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();
FileInputStream fis = new FileInputStream("list.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
List anotherList = (List) ois.readObject();
ois.close();
System.out.println(anotherList);
}
}
ObjectInputStream: close()
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] a) throws Exception {
List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
FileOutputStream fos = new FileOutputStream("list.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();
FileInputStream fis = new FileInputStream("list.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
List anotherList = (List) ois.readObject();
ois.close();
System.out.println(anotherList);
}
}
ObjectInputStream: readObject()
/*
Output:
[I, P, G, E]
[I, P, G, E]
* */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class MainClass {
public static void main(String args[]) throws Exception {
String elements[] = { "I", "P", "E", "G", "P" };
Set set = new HashSet(Arrays.asList(elements));
Set set2 = ((Set) ((HashSet) set).clone());
System.out.println(set2);
FileOutputStream fos = new FileOutputStream("set.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(set);
oos.close();
FileInputStream fis = new FileInputStream("set.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Set set3 = (Set) ois.readObject();
ois.close();
System.out.println(set3);
}
}