0

So i looked over a couple of solutions but none of them worked for me. Thanks for some folks i got the infinite cycle right in the end, but it still doesnt work for me. I want to read a file "output.txt" into a list of objects. So i provide the remaining codes since i thought they wouldnt help at all...

try {
            List<House> listH = new ArrayList<>();
            boolean cont = true;
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("output.txt"));
            while(cont)
            {
                House house = (House) ois.readObject();
                if(house != null)
                    listH.add(house);
                else
                    cont = false;
            }
            ois.close();
            mainmenu();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

The writing code:

try {
            FileWriter writer = new FileWriter("output.txt");
            for(House str : listH)
            {
                writer.write(String.valueOf(str) + "\n");
            }
            writer.close();
            System.out.println("Successful writing");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Couldn't write");
        }

The House Object:

public class House {
    String address;
    double area;
    boolean garage;

    public House(String address, double area, boolean garage){
        this.address=address;
        this.area = area;
        this.garage = garage;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public double getArea() {
        return area;
    }

    public void setArea(double area) {
        this.area = area;
    }

    public boolean isGarage() {
        return garage;
    }

    public void setGarage(boolean garage) {
        this.garage = garage;
    }

    @Override
    public String toString() {
        return address + ";" + area + ";" + garage;
    }
}

The error messages are the following:

java.io.StreamCorruptedException: invalid stream header: 3134313B at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:866) at java.io.ObjectInputStream.(ObjectInputStream.java:358)

5
  • Can you share sample House object information in txt file ? Commented Jun 9, 2019 at 10:06
  • An object Stream is a very specific binary format, not a text file. Commented Jun 9, 2019 at 10:09
  • Any comments on the answers you received so far? You see, all essential things have been said here. Commented Jun 9, 2019 at 13:01
  • I got it to work with a Scanner instead of ObjectInputStream Commented Jun 9, 2019 at 13:33
  • Then please consider accepting one of the answers. For example the one that tells you that you should carefully check whether you want to read TEXT or serialized binary objects ;-) Commented Jun 11, 2019 at 6:43

2 Answers 2

1

You have three main problems:

  1. Your while loop is infinite and may cause a stackoverflow error, make the condition check that there is another line/object to be read in the file before it proceeds.
  2. The code that reads the file casts directly to a House object, this highly dependent on format of the file writing, any error will cause a failed read.
  3. The else part of the condition checking if house is null breaks out of the whole reading process, so if there is one null house for any reason all houses after it will not be read For a more specific answer I may need House class structure and the process used to create the output file.Otherwise use hasnext in the while condition and readline to 'manually' construct a house object from string values.
Sign up to request clarification or add additional context in comments.

Comments

0

This here:

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("output.txt"));

is a contradiction in itself. You see, when you serialize java objects via the standard Java mechanism, you end up with bytes. Binary data.

Thus:

  • either your file has a wrong file extension (and is binary, not text) OR:
  • whatever you have in that file ... isn't binary data.

The exception implies that the second case is more likely.

In other words: verify what really is in that "output.txt" file. Obviously, not the result of serializing Java objects.

(note: of course, the file extension doesn't matter, but it implies that the file really is something different then what you expect it to be)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.