List of usage examples for java.io ObjectInputStream readObject
public final Object readObject() throws IOException, ClassNotFoundException
From source file:MainClass.java
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);/*from w w w . j a va2s .com*/ 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); }
From source file:MainClass.java
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);// www.j av a 2 s .c o m oos.close(); FileInputStream fis = new FileInputStream("list.ser"); ObjectInputStream ois = new ObjectInputStream(fis); List anotherList = (List) ois.readObject(); ois.close(); System.out.println(anotherList); }
From source file:Main.java
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);/*from w ww . j a v a 2s . c om*/ oos.close(); FileInputStream fis = new FileInputStream("list.ser"); ObjectInputStream ois = new ObjectInputStream(fis); List anotherList = (List) ois.readObject(); ois.close(); System.out.println(anotherList); }
From source file:MainClass.java
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);//www. ja v a 2 s .co m oos.close(); FileInputStream fis = new FileInputStream("list.ser"); ObjectInputStream ois = new ObjectInputStream(fis); List anotherList = (List) ois.readObject(); ois.close(); System.out.println(anotherList); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { FileInputStream fis = new FileInputStream("test"); ObjectInputStream ois = new ObjectInputStream(fis); Object o = ois.readObject(); if (!(o instanceof String)) { System.out.println("Unexpected data in file"); System.exit(-1);/*w ww .j a v a 2 s . co m*/ } String data = (String) o; System.out.println("Got message " + data); o = ois.readObject(); if (!(o instanceof byte[])) { System.out.println("Unexpected data in file"); System.exit(-1); } byte origDigest[] = (byte[]) o; byte pass[] = "aaa".getBytes(); byte buf[] = data.getBytes(); MessageDigest md = MessageDigest.getInstance("SHA"); md.update(pass); md.update(buf); byte digest1[] = md.digest(); md.update(pass); md.update(digest1); System.out.println(MessageDigest.isEqual(md.digest(), origDigest)); }
From source file:Main.java
public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("books.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); Book book = new Book("1", "Java", "A"); oos.writeObject(book);/* w ww . j a va 2s. co m*/ oos.flush(); oos.close(); FileInputStream fis = new FileInputStream("books.dat"); ObjectInputStream ois = new ObjectInputStream(fis); book = (Book) ois.readObject(); System.out.println(book.toString()); ois.close(); }
From source file:ObjectReader.java
public static void main(String[] arguments) { try {// www .j av a2 s. c o m FileInputStream fi = new FileInputStream("message.obj"); ObjectInputStream oi = new ObjectInputStream(fi); Message mess = (Message) oi.readObject(); System.out.println("Message:\n"); System.out.println("From: " + mess.from); System.out.println("To: " + mess.to); System.out.println("Date: " + mess.when + "\n"); for (int i = 0; i < mess.lineCount; i++) System.out.println(mess.text[i]); oi.close(); } catch (Exception e) { System.out.println("Error " + e.toString()); } }
From source file:InputOutputDemoObjectBinaryFile.java
public static void main(String[] a) throws Exception { //Write an object or array to binary file "java2sObject.dat": ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("java2sObject.dat")); oos.writeObject(new int[] { 2, 3, 5, 7, 11 }); oos.close();//from w ww . ja va 2s . c om //Read objects or arrays from binary file "o.dat": ObjectInputStream ois = new ObjectInputStream(new FileInputStream("java2sObject.dat")); int[] ia = (int[]) (ois.readObject()); System.out.println(ia[0] + "," + ia[1] + "," + ia[2] + "," + ia[3] + "," + ia[4]); }
From source file:ObjServer.java
public static void main(String[] args) throws Exception { Socket sock = new Socket(args[0], 1234); ObjectInputStream ois = new ObjectInputStream(sock.getInputStream()); Hashtable hash = (Hashtable) ois.readObject(); System.out.println(hash);//from w w w . j a va2 s . c o m ois.close(); sock.close(); }
From source file:Employee.java
public static void main(String[] args) throws Exception { Employee e1 = new Employee("A", 45000.0); System.out.println(e1.getName() + " " + e1.getSalary()); FileOutputStream fos = new FileOutputStream("employee.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(e1);/*from w w w.j a v a 2 s. c om*/ FileInputStream fis = new FileInputStream("employee.ser"); ObjectInputStream ois = new ObjectInputStream(fis); Employee e2 = (Employee) ois.readObject(); System.out.println(e2.getName() + " " + e2.getSalary()); }