List of usage examples for java.io ObjectOutputStream ObjectOutputStream
public ObjectOutputStream(OutputStream out) throws IOException
From source file:Data.java
public static void main(String[] args) { Data data = new Data(1); try {//from www . ja va 2s . c om ObjectOutputStream objectOut = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream("C:/test.bin"))); objectOut.writeObject(data); System.out.println("1st Object written has value: " + data.getValue()); data.setValue(2); objectOut.writeObject(data); System.out.println("2nd Object written has value: " + data.getValue()); data.setValue(3); objectOut.writeObject(data); System.out.println("3rd Object written has value: " + data.getValue()); objectOut.close(); } catch (IOException e) { e.printStackTrace(System.err); } try { ObjectInputStream objectIn = new ObjectInputStream( new BufferedInputStream(new FileInputStream("C:/test.bin"))); Data data1 = (Data) objectIn.readObject(); Data data2 = (Data) objectIn.readObject(); Data data3 = (Data) objectIn.readObject(); System.out.println(data1.equals(data2)); System.out.println(data2.equals(data3)); System.out.println(data1.getValue()); System.out.println(data2.getValue()); System.out.println(data3.getValue()); objectIn.close(); } catch (Exception e) { e.printStackTrace(System.err); } }
From source file:Person.java
public static void main(String[] args) { Person p1 = new Person("John", "Male", 1.7); Person p2 = new Person("Wally", "Male", 1.7); Person p3 = new Person("Katrina", "Female", 1.4); File fileObject = new File("person.ser"); try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileObject))) { oos.writeObject(p1);/* w w w . j a v a2 s.c o m*/ oos.writeObject(p2); oos.writeObject(p3); // Display the serialized objects on the standard output System.out.println(p1); System.out.println(p2); System.out.println(p3); } catch (IOException e) { e.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 w w . j a va2 s . c om 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:MyClass.java
public static void main(String[] argv) throws Exception { double v[] = { 1.1, 2.2, 3.3 }; double v2[] = { 9.0, 8.0, 7.7 }; MyClass obj1 = new MyClass("This is a test", v, "Test.txt"); MyClass obj2 = new MyClass("Alpha Beta Gamma", v2, "Sample.dat"); ObjectOutputStream fout = new ObjectOutputStream(new FileOutputStream("obj.dat")); System.out.println("obj1:\n" + obj1); fout.writeObject(obj1);/* w w w . j a v a 2 s. c om*/ System.out.println("obj2:\n" + obj2); fout.writeObject(obj2); fout.close(); ObjectInputStream fin = new ObjectInputStream(new FileInputStream("obj.dat")); MyClass inputObj; inputObj = (MyClass) fin.readObject(); System.out.println("First object:\n" + inputObj); inputObj = (MyClass) fin.readObject(); System.out.println("Second object:\n" + inputObj); fin.close(); }
From source file:CachedRS.java
public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream(CRS_FILE_LOC); ObjectInputStream in = new ObjectInputStream(fis); CachedRowSet crs = (CachedRowSet) in.readObject(); fis.close();/*from ww w . j a v a 2s . c o m*/ in.close(); Class.forName("oracle.jdbc.driver.OracleDriver"); crs.setUrl("jdbc:oracle:thin:@localhost:1521:ORCL"); crs.setUsername("yourName"); crs.setPassword("mypwd"); String sql = "SELECT SSN, Name, Salary, Hiredate FROM Employees WHERE SSN=?"; crs.setCommand(sql); crs.setInt(1, 111111111); crs.execute(); FileOutputStream fos = new FileOutputStream(CRS_FILE_LOC); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeObject(crs); out.close(); crs.close(); fis = new FileInputStream(CRS_FILE_LOC); in = new ObjectInputStream(fis); crs = (CachedRowSet) in.readObject(); fis.close(); in.close(); while (crs.next()) { System.out.print("SSN: " + crs.getInt("ssn")); System.out.print(", Name: " + crs.getString("name")); System.out.print(", Salary: $" + crs.getDouble("salary")); System.out.print(", HireDate: " + crs.getDate("hiredate")); } crs.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);// w ww .j a v a 2s .co m o1.writeObject(employees); 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:Engine.java
public static void main(String[] args) throws Exception { Car c1 = new Car("Some car", 4, new Engine(6)); ObjectOutputStream oos = null; FileOutputStream fos = new FileOutputStream("car.ser"); oos = new ObjectOutputStream(fos); oos.writeObject(c1);//w w w . ja va 2 s . c om ObjectInputStream ois = null; FileInputStream fis = new FileInputStream("car.ser"); ois = new ObjectInputStream(fis); Car c2 = (Car) ois.readObject(); System.out.println("Number of tires = " + c2.getNumTires()); System.out.println("Number of cylinders = " + c2.getEngine().getNumCylinders()); System.out.println("Name = " + c2.getName()); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory(); SSLSocket socket = (SSLSocket) factory.createSocket("127.0.0.1", 9999); socket.startHandshake();/*from w w w . j a va 2 s. c o m*/ SSLSession session = socket.getSession(); java.security.cert.Certificate[] servercerts = session.getPeerCertificates(); List mylist = new ArrayList(); for (int i = 0; i < servercerts.length; i++) { mylist.add(servercerts[i]); } CertificateFactory cf = CertificateFactory.getInstance("X.509"); CertPath cp = cf.generateCertPath(mylist); FileOutputStream f = new FileOutputStream("CertPath.dat"); ObjectOutputStream b = new ObjectOutputStream(f); b.writeObject(cp); }
From source file:Person.java
public static void main(String[] args) throws Exception { ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat")); Person person = new Person(); person.setFirstName("A"); person.setLastName("B"); person.setAge(38);/*from w ww . j a va2 s .co m*/ outputStream.writeObject(person); person = new Person(); person.setFirstName("C"); person.setLastName("D"); person.setAge(22); outputStream.writeUnshared(person); outputStream.close(); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); random.setSeed(101L);/*from w w w . j a va2 s .c om*/ keyGen.initialize(1024, random); KeyPair keypair = keyGen.generateKeyPair(); KeyFactory kfactory = KeyFactory.getInstance("DSA"); DSAPublicKeySpec kspec = (DSAPublicKeySpec) kfactory.getKeySpec(keypair.getPublic(), DSAPublicKeySpec.class); System.out.println(keypair.getPublic()); FileOutputStream fos = new FileOutputStream("publicKeys"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(kspec.getY()); oos.writeObject(kspec.getP()); oos.writeObject(kspec.getQ()); oos.writeObject(kspec.getG()); FileInputStream fin = new FileInputStream("publicKeys"); ObjectInputStream ois = new ObjectInputStream(fin); BigInteger Y = (BigInteger) ois.readObject(); BigInteger P = (BigInteger) ois.readObject(); BigInteger Q = (BigInteger) ois.readObject(); BigInteger G = (BigInteger) ois.readObject(); DSAPublicKeySpec keyspec = new DSAPublicKeySpec(Y, P, Q, G); PublicKey pkey = kfactory.generatePublic(keyspec); System.out.println(pkey); }