Save different type of objects and read back in Java

Description

The following code shows how to save different type of objects and read back.

Example


//from  w  ww .j  ava2 s  .  co  m
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

public class Main {
  public static void main(String args[]) throws IOException, ClassNotFoundException {
    File file = new File("test.txt");
    FileOutputStream outFile = new FileOutputStream(file);
    ObjectOutputStream outStream = new ObjectOutputStream(outFile);
    TestClass1 t1 = new TestClass1(true, 9, 'A', 0.0001, "java");
    TestClass2 t2 = new TestClass2();
    String t3 = "This is a test.";
    Date t4 = new Date();
    outStream.writeObject(t1);
    outStream.writeObject(t2);
    outStream.writeObject(t3);
    outStream.writeObject(t4);
    outStream.close();
    outFile.close();
    FileInputStream inFile = new FileInputStream(file);
    ObjectInputStream inStream = new ObjectInputStream(inFile);
    System.out.println(inStream.readObject());
    System.out.println(inStream.readObject());
    System.out.println(inStream.readObject());
    System.out.println(inStream.readObject());
    inStream.close();
    inFile.close();
    file.delete();
  }
}

class TestClass1 implements Serializable {
  boolean b;

  int i;

  char c;

  double d;

  String s;

  TestClass1(boolean b, int i, char c, double d, String s) {
    this.b = b;
    this.i = i;
    this.c = c;
    this.d = d;
    this.s = s;
  }

  public String toString() {
    String r = String.valueOf(b) + " ";
    r += String.valueOf(i) + " ";
    r += String.valueOf(c) + " ";
    r += String.valueOf(d) + " ";
    r += String.valueOf(s);
    return r;
  }
}

class TestClass2 implements Serializable {
  int i;

  TestClass1 tc1;

  TestClass1 tc2;

  TestClass2() {
    i = 0;
    tc1 = new TestClass1(true, 2, 'j', 1.234, "Java");
    tc2 = new TestClass1(false, 7, 'J', 2.468, "JAVA");
  }

  public String toString() {
    String r = String.valueOf(i) + " ";
    r += tc1.toString() + " ";
    r += tc2.toString();
    return r;
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip