1

I'm new to Programming world and was exploring JAVA File I/O operation but found strange thing that when I'm trying to read ObjectOutputStream generated file from File System it is showing Byte Array sort of data instead of text redable data.

Please note, it is allowing me to read data using ObjectInputStream but not from file system directly.

Code I'm using:(reference: https://www.tutorialspoint.com/java/java_serialization.htm)

public class Employee implements java.io.Serializable {
   public String name;
   
   public void mailCheck() {
      System.out.println("Mailing a check to " + name);
   }
}

public class SerializeDemo {

   public static void main(String [] args) {
      Employee e = new Employee();
      e.name = "Reyan Ali";
      
      try {
         FileOutputStream fileOut =
         new FileOutputStream("C:/Users/123/Documents/record.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(e);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved.");
      } catch (IOException i) {
         i.printStackTrace();
      }
   }
}
  • Machine Configuration : Window 10 | JDK 1.8

When I open file in text editor this is how it look like:

aced 0005 7372 0008 456d 706c 6f79 6565
025e 7434 67c6 123c 0200 0349 0006 6e75
....7274 0009 5265 7961 6e20 416c 69 

Any suggestion how to read ObjectOutputStream file in text editor?

DevNadim
  • 31
  • 4

2 Answers2

0

Generally you don't. It is binary file.

You can find details about format in Java documentation. But you will need more than simple text editor to be able to decipher it.

I suggest you to chose different storage format. There are plenty to choose from. XML and JSON probably most popular. All of them have their own advantages and disadvantages.

talex
  • 17,973
  • 3
  • 29
  • 66
0

You need to use ObjectInputStream! Read de file with FileInputStream to get ois

FileInputStream fis = new FileInputStream(new File());
ObjectInputStram ois = new ObjectInputStram(fis));
Employee e = (Enployee) ois.readObject();