-2

I'm trying to read and write objects into a file. Reading the output into a new object works, but every value is null.

Here's the code:

public void read() throws Exception
    {
        try
        {
            FileInputStream fIn = new FileInputStream(file);
            ObjectInputStream in = new ObjectInputStream(fIn);

            Object obj = in.readObject();

            System.out.println(obj);
public void save() throws Exception
    {
        FileOutputStream fOut = new FileOutputStream(file.toString());
        ObjectOutputStream out = new ObjectOutputStream(fOut);

        out.writeObject(this);
        out.flush();
        out.close();
    }

Here is the file output: (image of output)

I'd like to receive the values I previously wrote to the file in the new object created, however all I get is null for all values.

Edit: since people are asking for the entire class, and I have no idea what code could be causing what, here's the entire UserFile class: https://pastebin.com/Gr1tcGsg

geger42 _
  • 15
  • 5
  • 3
    Is the new object of the right class? Also show the source of the class that you have serialized. You may have mistakes in it. – Erwin Bolwidt Apr 08 '19 at 05:00
  • NB You don't need `file.toString()`. Just `file` will do. You also don't need to flush before closing. – user207421 Apr 08 '19 at 05:24
  • @user207421 close calls flush, and ye, he is flushing before closing anyway. – Antoniossss Apr 08 '19 at 05:28
  • OK i read that he doesn't flush. As a good practice with object stream its good to flush after write to push headers. But that is general rule of particular stream implementation. – Antoniossss Apr 08 '19 at 05:31
  • No they are not. That is general rule for ObjectOutputStream. – Antoniossss Apr 08 '19 at 05:32
  • @Antoniossss No what are not what? If you're still trying to educate me about how `FilterOutputStream.close()` works in Java, don't bother. I learnt it in 1997, and it's all documented anyway. – user207421 Apr 08 '19 at 05:33
  • @Antoniossss I don't know what you're trying to contribute here that hasn't already been said. Don't make personal remarks here. – user207421 Apr 08 '19 at 07:04

2 Answers2

0

I have ran that code and it works which means you most likely read before you write or you got an exception such as InvalidClassException: no valid constructor which would make sense in your case.

The code I ran:

public class SavedObject implements Serializable
{
    public static void main(String[] args) throws IOException, ClassNotFoundException
    {
        new SavedObject();
    }

    private final int random;

    private SavedObject() throws IOException, ClassNotFoundException
    {
        random = ThreadLocalRandom.current().nextInt();
        File file = new File("Object.txt");
        save(file);
        read(file);
    }

    private void save(File file) throws IOException
    {
        FileOutputStream fileOutput = new FileOutputStream(file);
        ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);

        objectOutput.writeObject(this);
        objectOutput.close();
        fileOutput.close();

        System.out.println(this);
    }

    private void read(File file) throws IOException, ClassNotFoundException
    {
        FileInputStream fileInput = new FileInputStream(file);
        ObjectInputStream objectInput = new ObjectInputStream(fileInput);

        Object obj = objectInput.readObject();

        System.out.println(obj);

        objectInput.close();
        fileInput.close();
    }

    public String toString()
    {
        return "SavedObject(Random: " + random + ")";
    }
}

Which prints:

SavedObject(Random: -2145716528)
SavedObject(Random: -2145716528)

Also a few tips for you:

  • Don't have a try-catch if you throws
  • Have more readable variable names
  • Send more code in your next question
  • ObjectOutputStream is not recommended, if you can, you should write the values as they are
  • Don't use throws "Exception" instead use throws "
  • Put the file in instead of file.toString()
OughtToPrevail
  • 380
  • 3
  • 10
0

I found the problem, it was that in my constructor I wasn't correctly applying the retrieved info from the file. Thanks for everyone's help.

geger42 _
  • 15
  • 5