List of usage examples for java.io ObjectOutputStream writeObject
public final void writeObject(Object obj) throws IOException
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); FileInputStream fis = new FileInputStream("employee.ser"); ObjectInputStream ois = new ObjectInputStream(fis); Employee e2 = (Employee) ois.readObject(); System.out.println(e2.getName() + " " + e2.getSalary()); }
From source file:MainClass.java
public static void main(String[] args) { Employee e = new Employee(); e.name = "Jor Lee"; e.address = "USA"; e.SSN = 11122333;/* ww w . j a v a 2 s .co m*/ e.number = 101; try { FileOutputStream fileOut = new FileOutputStream("employee.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); } catch (IOException i) { i.printStackTrace(); } }
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("DES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); random.setSeed(101L);/*w ww . j a v a 2 s . c o m*/ keyGen.init(56, random); SecretKey sKey = keyGen.generateKey(); SecretKeyFactory kfactory = SecretKeyFactory.getInstance("DES"); DESKeySpec kspec = (DESKeySpec) kfactory.getKeySpec(sKey, DESKeySpec.class); System.out.println(sKey); FileOutputStream fos = new FileOutputStream("secretKeys"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(kspec.getKey()); FileInputStream fin = new FileInputStream("secretKeys"); ObjectInputStream ois = new ObjectInputStream(fin); byte[] kMaterial = (byte[]) ois.readObject(); DESKeySpec keyspec = new DESKeySpec(kMaterial); SecretKey newKey = kfactory.generateSecret(keyspec); System.out.println(newKey); System.out.println("Do the keys equal :" + newKey.equals(sKey)); }
From source file:Main.java
public static void main(String[] args) throws Exception { FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); oout.writeObject(new Example()); oout.flush();//from w ww .j ava2 s . com oout.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); Example a = (Example) ois.readObject(); System.out.println(a.s); a.validateObject(); ois.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); oout.writeObject(new Example()); oout.flush();/* ww w .j a v a2 s .c o m*/ oout.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); Example a = (Example) ois.readObject(); System.out.println(a.isDefault); System.out.println(a.string); ois.close(); }
From source file:ObjServer.java
public static void main(String args[]) throws Exception { ServerSocket ssock = new ServerSocket(1234); Hashtable hash = new Hashtable(); hash.put("Java Source and Support", "www.java2s.com"); while (true) { System.out.println("Listening"); Socket sock = ssock.accept(); ObjectOutputStream ostream = new ObjectOutputStream(sock.getOutputStream()); ostream.writeObject(hash); ostream.close();/*from ww w.java 2 s. com*/ sock.close(); } }
From source file:ID.java
public static void main(String[] args) throws IOException, ClassNotFoundException { ID id = new ID(); List employees = new ArrayList(); employees.add(new Employee("A", id)); employees.add(new Employee("B", id)); employees.add(new Employee("C", id)); System.out.println("employees: " + employees); ByteArrayOutputStream buf1 = new ByteArrayOutputStream(); ObjectOutputStream o1 = new ObjectOutputStream(buf1); o1.writeObject(employees); o1.writeObject(employees);/*from w ww . j ava 2 s. c o m*/ ByteArrayOutputStream buf2 = new ByteArrayOutputStream(); ObjectOutputStream o2 = new ObjectOutputStream(buf2); o2.writeObject(employees); ObjectInputStream in1 = new ObjectInputStream(new ByteArrayInputStream(buf1.toByteArray())); ObjectInputStream in2 = new ObjectInputStream(new ByteArrayInputStream(buf2.toByteArray())); List emp1 = (List) in1.readObject(), emp2 = (List) in1.readObject(), emp3 = (List) in2.readObject(); System.out.println("emp1: " + emp1); System.out.println("emp2: " + emp2); System.out.println("emp3: " + emp3); }
From source file:Main.java
public static void main(String args[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String URL = "jdbc:odbc:dbname"; Connection dbConn = DriverManager.getConnection(URL, "user", "passw"); Employee employee = new Employee(42, "AA", 9); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(employee); byte[] employeeAsBytes = baos.toByteArray(); PreparedStatement pstmt = dbConn.prepareStatement("INSERT INTO EMPLOYEE (emp) VALUES(?)"); ByteArrayInputStream bais = new ByteArrayInputStream(employeeAsBytes); pstmt.setBinaryStream(1, bais, employeeAsBytes.length); pstmt.executeUpdate();/*from ww w. j av a 2 s . c o m*/ pstmt.close(); Statement stmt = dbConn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT emp FROM Employee"); while (rs.next()) { byte[] st = (byte[]) rs.getObject(1); ByteArrayInputStream baip = new ByteArrayInputStream(st); ObjectInputStream ois = new ObjectInputStream(baip); Employee emp = (Employee) ois.readObject(); } stmt.close(); rs.close(); dbConn.close(); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyGenerator kg = KeyGenerator.getInstance("DES"); kg.init(new SecureRandom()); SecretKey key = kg.generateKey(); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); Class spec = Class.forName("javax.crypto.spec.DESKeySpec"); DESKeySpec ks = (DESKeySpec) skf.getKeySpec(key, spec); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("keyfile")); oos.writeObject(ks.getKey()); Cipher c = Cipher.getInstance("DES/CFB8/NoPadding"); c.init(Cipher.ENCRYPT_MODE, key); CipherOutputStream cos = new CipherOutputStream(new FileOutputStream("ciphertext"), c); PrintWriter pw = new PrintWriter(new OutputStreamWriter(cos)); pw.println("Stand and unfold yourself"); pw.close();//from w w w . j av a2 s . co m oos.writeObject(c.getIV()); oos.close(); }
From source file:House.java
public static void main(String[] args) throws IOException, ClassNotFoundException { House house = new House(); List animals = new ArrayList(); animals.add(new Animal("Bosco the dog", house)); animals.add(new Animal("Ralph the hamster", house)); animals.add(new Animal("Fronk the cat", house)); System.out.println("animals: " + animals); ByteArrayOutputStream buf1 = new ByteArrayOutputStream(); ObjectOutputStream o1 = new ObjectOutputStream(buf1); o1.writeObject(animals); o1.writeObject(animals); // Write a 2nd set // Write to a different stream: ByteArrayOutputStream buf2 = new ByteArrayOutputStream(); ObjectOutputStream o2 = new ObjectOutputStream(buf2); o2.writeObject(animals);//ww w.j a v a 2 s . c om // Now get them back: ObjectInputStream in1 = new ObjectInputStream(new ByteArrayInputStream(buf1.toByteArray())); ObjectInputStream in2 = new ObjectInputStream(new ByteArrayInputStream(buf2.toByteArray())); List animals1 = (List) in1.readObject(), animals2 = (List) in1.readObject(), animals3 = (List) in2.readObject(); System.out.println("animals1: " + animals1); System.out.println("animals2: " + animals2); System.out.println("animals3: " + animals3); }