List of usage examples for java.io ObjectOutputStream ObjectOutputStream
public ObjectOutputStream(OutputStream out) throws IOException
From source file:Main.java
public static void main(String[] args) throws Exception { String s = "Hello World from java2s.com"; FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeUTF(s);/* w w w. j a va2s .c o m*/ oout.flush(); oout.close(); // create an ObjectInputStream for the file we created before Main ois = new Main(new FileInputStream("test.txt")); // read and print an object and cast it as string System.out.println((String) ois.readUTF()); // get the class for string and print the name ObjectStreamClass streamClass = ObjectStreamClass.lookup(Integer.class); System.out.println(ois.resolveClass(streamClass).getName()); ois.close(); }
From source file:ObjectWriter.java
public static void main(String[] arguments) { Message mess = new Message(); String author = "London"; String recipient = "G, B"; String[] letter = { "Merry Christmas." }; Date now = new Date(); mess.writeMessage(author, recipient, now, letter); try {/* w w w . j a va 2 s .c o m*/ FileOutputStream fo = new FileOutputStream("Message.obj"); ObjectOutputStream oo = new ObjectOutputStream(fo); oo.writeObject(mess); oo.close(); System.out.println("Object created successfully."); } catch (IOException e) { System.out.println("Error - " + e.toString()); } }
From source file:LoopingSocketServer.java
public static void main(String args[]) throws Exception { ServerSocket servSocket;/*ww w. j a va 2s. co m*/ 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:SerialDemo.java
static public void main(String[] args) { try {//from www. j a va 2 s. c om { // 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: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 www . j a va 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:SimpleSocketServer.java
public static void main(String args[]) throws Exception { ServerSocket serverSocket;/* w w w. j a v a2 s.c o m*/ int portNumber = 1777; Socket socket; String str; str = " <?xml version=\"1.0\" encoding=\"UTF-8\"?>"; str += "<ticketRequest><customer custID=\"1\">"; str += "</ticketRequest>"; serverSocket = new ServerSocket(portNumber); System.out.println("Waiting for a connection on " + portNumber); socket = serverSocket.accept(); ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); oos.writeObject(str); oos.close(); socket.close(); }
From source file:EmployeeInit.java
public static void main(String[] args) throws Exception { Connection con;// w w w .ja v a 2s . c o m con = DriverManager.getConnection("jdbc:derby://localhost:1527/" + "c:\\db\\employee"); PreparedStatement ps; ps = con.prepareStatement("insert into employee(name,photo) " + "values(?,?)"); ps.setString(1, "Duke"); Blob blob = con.createBlob(); ImageIcon ii = new ImageIcon("duke.png"); ObjectOutputStream oos; oos = new ObjectOutputStream(blob.setBinaryStream(1)); oos.writeObject(ii); oos.close(); ps.setBlob(2, blob); ps.execute(); blob.free(); ps.close(); }
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);/* w w w . j a v a2s . c o m*/ 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(); 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:Employee.java
public static void main(String[] args) { Employee e = new Employee(); e.name = "A"; e.address = "B"; e.SSN = 11111;/*from w ww . ja va2s. c o 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 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());// w ww .ja v a 2 s.c o m 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(); oos.writeObject(c.getIV()); oos.close(); }