List of usage examples for java.io ObjectOutputStream writeObject
public final void writeObject(Object obj) throws IOException
From source file:Blip3.java
public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println("Constructing objects:"); Blip3 b3 = new Blip3("A String ", 47); System.out.println(b3);//w w w. jav a 2 s. co m ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Blip3.out")); System.out.println("Saving object:"); o.writeObject(b3); o.close(); // Now get it back: ObjectInputStream in = new ObjectInputStream(new FileInputStream("Blip3.out")); System.out.println("Recovering b3:"); b3 = (Blip3) in.readObject(); System.out.println(b3); }
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 ava 2 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: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); System.out.println("obj2:\n" + obj2); fout.writeObject(obj2);//from w w w.j a va 2s.c o m 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:Blip1.java
public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println("Constructing objects:"); Blip1 b1 = new Blip1(); Blip2 b2 = new Blip2(); ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Blips.out")); System.out.println("Saving objects:"); o.writeObject(b1); o.writeObject(b2);/*from w w w . j a v a2 s. c o m*/ o.close(); // Now get them back: ObjectInputStream in = new ObjectInputStream(new FileInputStream("Blips.out")); System.out.println("Recovering b1:"); b1 = (Blip1) in.readObject(); // OOPS! Throws an exception: //! System.out.println("Recovering b2:"); //! b2 = (Blip2)in.readObject(); }
From source file:com.textocat.textokit.morph.opencorpora.resource.XmlDictionaryPSP.java
public static void main(String[] args) throws Exception { XmlDictionaryPSP cfg = new XmlDictionaryPSP(); new JCommander(cfg, args); MorphDictionaryImpl dict = new MorphDictionaryImpl(); DictionaryExtension ext = cfg.dictExtensionClass.newInstance(); FileInputStream fis = FileUtils.openInputStream(cfg.dictXmlFile); try {/*from ww w.j a v a2 s . c o m*/ new XmlDictionaryParser(dict, ext, fis).run(); } finally { IOUtils.closeQuietly(fis); } System.out.println("Preparing to serialization..."); long timeBefore = currentTimeMillis(); OutputStream fout = new BufferedOutputStream(FileUtils.openOutputStream(cfg.outputJarFile), 8192 * 8); Manifest manifest = new Manifest(); manifest.getMainAttributes().putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0"); manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_VERSION, dict.getVersion()); manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_REVISION, dict.getRevision()); manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_VARIANT, cfg.variant); String dictEntryName = String.format( OpencorporaMorphDictionaryAPI.FILENAME_PATTERN_OPENCORPORA_SERIALIZED_DICT, dict.getVersion(), dict.getRevision(), cfg.variant); JarOutputStream jarOut = new JarOutputStream(fout, manifest); jarOut.putNextEntry(new ZipEntry(dictEntryName)); ObjectOutputStream serOut = new ObjectOutputStream(jarOut); try { serOut.writeObject(dict.getGramModel()); serOut.writeObject(dict); } finally { serOut.flush(); jarOut.closeEntry(); serOut.close(); } System.out.println(String.format("Serialization finished in %s ms.\nOutput size: %s bytes", currentTimeMillis() - timeBefore, cfg.outputJarFile.length())); }
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 ww w .ja va 2 s .c o m*/ 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: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); ObjectInputStream ois = null; FileInputStream fis = new FileInputStream("car.ser"); ois = new ObjectInputStream(fis); Car c2 = (Car) ois.readObject();//from ww w. ja v a2 s . c o m 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:TestCipher.java
public static void main(String args[]) throws Exception { Set set = new HashSet(); Random random = new Random(); for (int i = 0; i < 10; i++) { Point point = new Point(random.nextInt(1000), random.nextInt(2000)); set.add(point);//from w w w.j a v a2 s. co m } int last = random.nextInt(5000); // Create Key byte key[] = password.getBytes(); DESKeySpec desKeySpec = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); // Create Cipher Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); desCipher.init(Cipher.ENCRYPT_MODE, secretKey); // Create stream FileOutputStream fos = new FileOutputStream("out.des"); BufferedOutputStream bos = new BufferedOutputStream(fos); CipherOutputStream cos = new CipherOutputStream(bos, desCipher); ObjectOutputStream oos = new ObjectOutputStream(cos); // Write objects oos.writeObject(set); oos.writeInt(last); oos.flush(); oos.close(); // Change cipher mode desCipher.init(Cipher.DECRYPT_MODE, secretKey); // Create stream FileInputStream fis = new FileInputStream("out.des"); BufferedInputStream bis = new BufferedInputStream(fis); CipherInputStream cis = new CipherInputStream(bis, desCipher); ObjectInputStream ois = new ObjectInputStream(cis); // Read objects Set set2 = (Set) ois.readObject(); int last2 = ois.readInt(); ois.close(); // Compare original with what was read back int count = 0; if (set.equals(set2)) { System.out.println("Set1: " + set); System.out.println("Set2: " + set2); System.out.println("Sets are okay."); count++; } if (last == last2) { System.out.println("int1: " + last); System.out.println("int2: " + last2); System.out.println("ints are okay."); count++; } if (count != 2) { System.out.println("Problem during encryption/decryption"); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String WRITE_OBJECT_SQL = "BEGIN " + " INSERT INTO java_objects(object_id, object_name, object_value) " + " VALUES (?, ?, empty_blob()) " + " RETURN object_value INTO ?; " + "END;"; String READ_OBJECT_SQL = "SELECT object_value FROM java_objects WHERE object_id = ?"; Connection conn = getOracleConnection(); conn.setAutoCommit(false);/*from ww w. j ava2 s .c o m*/ List<Object> list = new ArrayList<Object>(); list.add("This is a short string."); list.add(new Integer(1234)); list.add(new java.util.Date()); // write object to Oracle long id = 0001; String className = list.getClass().getName(); CallableStatement cstmt = conn.prepareCall(WRITE_OBJECT_SQL); cstmt.setLong(1, id); cstmt.setString(2, className); cstmt.registerOutParameter(3, java.sql.Types.BLOB); cstmt.executeUpdate(); BLOB blob = (BLOB) cstmt.getBlob(3); OutputStream os = blob.getBinaryOutputStream(); ObjectOutputStream oop = new ObjectOutputStream(os); oop.writeObject(list); oop.flush(); oop.close(); os.close(); // Read object from oracle PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL); pstmt.setLong(1, id); ResultSet rs = pstmt.executeQuery(); rs.next(); InputStream is = rs.getBlob(1).getBinaryStream(); ObjectInputStream oip = new ObjectInputStream(is); Object object = oip.readObject(); className = object.getClass().getName(); oip.close(); is.close(); rs.close(); pstmt.close(); conn.commit(); // de-serialize list a java object from a given objectID List listFromDatabase = (List) object; System.out.println("[After De-Serialization] list=" + listFromDatabase); conn.close(); }
From source file:AESTest.java
public static void main(String[] args) { try {//from w w w . ja v a2 s . com if (args[0].equals("-genkey")) { KeyGenerator keygen = KeyGenerator.getInstance("AES"); SecureRandom random = new SecureRandom(); keygen.init(random); SecretKey key = keygen.generateKey(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1])); out.writeObject(key); out.close(); } else { int mode; if (args[0].equals("-encrypt")) mode = Cipher.ENCRYPT_MODE; else mode = Cipher.DECRYPT_MODE; ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3])); Key key = (Key) keyIn.readObject(); keyIn.close(); InputStream in = new FileInputStream(args[1]); OutputStream out = new FileOutputStream(args[2]); Cipher cipher = Cipher.getInstance("AES"); cipher.init(mode, key); crypt(in, out, cipher); in.close(); out.close(); } } catch (IOException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }