List of usage examples for java.io ObjectOutputStream close
public void close() throws IOException
From source file:Main.java
public static void main(String[] args) throws Exception { FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeUTF("Hello World from java2s.com"); oout.flush();/* ww w.j a v a 2 s . co m*/ oout.close(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // check how many bytes are available System.out.println(ois.available()); ois.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("books.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); Book book = new Book("1", "Java", "A"); oos.writeObject(book);//from w ww .j a v a 2 s . c o m oos.flush(); oos.close(); FileInputStream fis = new FileInputStream("books.dat"); ObjectInputStream ois = new ObjectInputStream(fis); book = (Book) ois.readObject(); System.out.println(book.toString()); ois.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { ObjectOutputStream out = new ObjectOutputStream( new BufferedOutputStream(new FileOutputStream("file.data"))); out.write("java2s.com".getBytes(), 1, 2); out.close(); ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data"))); byte[] byteArray = new byte[10]; in.read(byteArray);/*from ww w . j a v a2s . c om*/ System.out.println(Arrays.toString(byteArray)); in.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { int i = 123456; FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeInt(i);//from ww w.ja va 2 s . c o m oout.writeInt(54321); 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 int System.out.println(ois.readInt()); // read and print an int System.out.println(ois.readInt()); ois.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { char b = 'F'; FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeChar(b);/*from ww w . j a va 2 s. c o m*/ oout.writeChar('A'); oout.flush(); oout.close(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // read and print a char System.out.println(ois.readChar()); // read and print a char System.out.println(ois.readChar()); ois.close(); }
From source file:cn.lynx.emi.license.GenerateLicense.java
public static void main(String[] args) throws ClassNotFoundException, ParseException { if (args == null || args.length != 4) { System.err.println("Please provide [machine code] [cpu] [memory in gb] [yyyy-MM-dd] as parameter"); return;/*from w w w.j av a 2 s .c o m*/ } InputStream is = GenerateLicense.class.getResourceAsStream("/privatekey"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String key = null; try { key = br.readLine(); } catch (IOException e) { System.err.println( "Can't read the private key file, make sure there's a file named \"privatekey\" in the root classpath"); e.printStackTrace(); return; } if (key == null) { System.err.println( "Can't read the private key file, make sure there's a file named \"privatekey\" in the root classpath"); return; } String machineCode = args[0]; int cpu = Integer.parseInt(args[1]); long mem = Long.parseLong(args[2]) * 1024 * 1024 * 1024; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); long expDate = sdf.parse(args[3]).getTime(); LicenseBean lb = new LicenseBean(); lb.setCpuCount(cpu); lb.setMemCount(mem); lb.setMachineCode(machineCode); lb.setExpireDate(expDate); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(baos); os.writeObject(lb); os.close(); String serializedLicense = Base64.encodeBase64String(baos.toByteArray()); System.out.println("License:" + encrypt(key, serializedLicense)); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { long l = 12345678909876L; FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeLong(l);//w w w .j a v a 2 s . com oout.writeLong(987654321L); oout.flush(); oout.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); System.out.println(ois.readLine()); ois.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { byte b = 12;// ww w . j av a 2 s . c om FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeByte(b); oout.writeByte(21); oout.flush(); oout.close(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // read and print a byte System.out.println((char) ois.readByte()); // read and print a byte System.out.println((char) ois.readByte()); ois.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { long l = 12345678909876L; FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeLong(l);//ww w . j a v a2s.c om oout.writeLong(987654321L); oout.flush(); oout.close(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // read and print a long System.out.println(ois.readLong()); // read and print a long System.out.println(ois.readLong()); ois.close(); }
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 ww . ja va2s .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); }