List of usage examples for java.io ObjectInputStream ObjectInputStream
public ObjectInputStream(InputStream in) throws IOException
From source file:Main.java
public static void main(String[] args) throws Exception { String s = "Hello World from java2s.com"; byte[] b = { 'e', 'x', 'a', 'm', 'p', 'l', 'e' }; FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeObject(s);/*w w w . j a v a2 s. co m*/ oout.writeObject(b); oout.flush(); oout.close(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // read and print an object and cast it as string System.out.println((String) ois.readObject()); // read and print an object and cast it as string byte[] read = (byte[]) ois.readObject(); String s2 = new String(read); System.out.println(s2); ois.close(); }
From source file:LoopingSocketServer.java
public static void main(String args[]) throws Exception { ServerSocket servSocket;//from w w w .java2 s .c om Socket fromClientSocket; int cTosPortNumber = 1777; String str; servSocket = new ServerSocket(cTosPortNumber); System.out.println("Waiting for a connection on " + cTosPortNumber); fromClientSocket = servSocket.accept(); System.out.println("fromClientSocket accepted"); ObjectOutputStream oos = new ObjectOutputStream(fromClientSocket.getOutputStream()); ObjectInputStream ois = new ObjectInputStream(fromClientSocket.getInputStream()); while ((str = (String) ois.readObject()) != null) { System.out.println("The message from client: " + str); if (str.equals("bye")) { oos.writeObject("bye bye"); break; } else { str = "Server returns " + str; oos.writeObject(str); } } oos.close(); fromClientSocket.close(); }
From source file:MainClass.java
public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println("Constructing objects:"); MyBean myBean = new MyBean("A String ", 47); System.out.println(myBean);/*from w w w . j ava2 s. c o m*/ ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("MyBean.out")); System.out.println("Saving object:"); o.writeObject(myBean); o.close(); ObjectInputStream in = new ObjectInputStream(new FileInputStream("MyBean.out")); System.out.println("Recovering:"); myBean = (MyBean) in.readObject(); System.out.println(myBean); }
From source file:Main.java
public static void main(String[] args) throws Exception { ObjectOutputStream out = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream("file.data"))); out.writeObject(Calendar.getInstance()); out.writeObject(new BigDecimal("123.123")); out.writeInt(1);//from w w w . j a v a 2 s . c om out.writeUTF("tutorial"); out.close(); ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data"))); BigDecimal price; int unit; String desc; Calendar date = (Calendar) in.readObject(); System.out.println(date); price = (BigDecimal) in.readObject(); unit = in.readInt(); desc = in.readUTF(); System.out.println(unit); System.out.println(desc); System.out.println(price); in.close(); }
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);/* w w w .j ava 2 s.c o m*/ 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:Main.java
public static void main(String[] args) { int[] x = new int[] { 1, 2, 3 }; int[] y = new int[] { 4, 5, 6 }; Polygon polygon = new Polygon(x, y, x.length); try {/*from w ww . java2s . c om*/ ObjectOutputStream objectOut = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream("Polygons.bin"))); objectOut.writeObject(polygon); objectOut.close(); } catch (IOException e) { e.printStackTrace(System.err); } try { ObjectInputStream objectIn = new ObjectInputStream( new BufferedInputStream(new FileInputStream("Polygons.bin"))); Polygon theLine = (Polygon) objectIn.readObject(); System.out.println(theLine); objectIn.close(); } catch (Exception e) { e.printStackTrace(System.err); } }
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);/* ww w .j av a2s . c o m*/ ois.close(); sock.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Junk obj1 = new Junk("A"); Junk obj2 = new Junk("B"); Junk obj3 = new Junk("V"); ObjectOutputStream objectOut = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream("C:/JunkObjects.bin"))); objectOut.writeObject(obj1); // Write object objectOut.writeObject(obj2); // Write object objectOut.writeObject(obj3); // Write object objectOut.close(); // Close the output stream ObjectInputStream objectIn = null; int objectCount = 0; Junk object = null;//from www. j a v a 2s .com objectIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream("C:/JunkObjects.bin"))); // Read from the stream until we hit the end while (objectCount < 3) { object = (Junk) objectIn.readObject(); objectCount++; System.out.println(object); } objectIn.close(); }
From source file:SerialDemo.java
static public void main(String[] args) { try {/*from ww w . j a v a2 s .co m*/ { // Save a SerialDemo object with a value of 5. FileOutputStream f = new FileOutputStream("/tmp/testing"); ObjectOutputStream s = new ObjectOutputStream(f); SerialDemo d = new SerialDemo(5); s.writeObject(d); s.flush(); } { // Now restore it and look at the value. FileInputStream f = new FileInputStream("/tmp/testing"); ObjectInputStream s = new ObjectInputStream(f); SerialDemo d = (SerialDemo) s.readObject(); System.out.println("SerialDemo.getVal() is: " + d.getVal()); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) throws IOException, ClassNotFoundException { File file = new File("test.txt"); FileOutputStream outFile = new FileOutputStream(file); ObjectOutputStream outStream = new ObjectOutputStream(outFile); TestClass1 t1 = new TestClass1(true, 9, 'A', 0.0001, "java"); TestClass2 t2 = new TestClass2(); String t3 = "This is a test."; Date t4 = new Date(); outStream.writeObject(t1);//from w w w . j av a 2s. c om outStream.writeObject(t2); outStream.writeObject(t3); outStream.writeObject(t4); outStream.close(); outFile.close(); FileInputStream inFile = new FileInputStream(file); ObjectInputStream inStream = new ObjectInputStream(inFile); System.out.println(inStream.readObject()); System.out.println(inStream.readObject()); System.out.println(inStream.readObject()); System.out.println(inStream.readObject()); inStream.close(); inFile.close(); file.delete(); }