0

I'm not sure what happened when saving a file, but now when I try to load it I get java.io.EOFException. All of the code is in try/catch blocks but there was no error when saving the file.

Opening the file:

FileInputStream fileInputStream = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);  
ArrayList<Tournament> allTournaments = (ArrayList<Tournament>) objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();

The file is an ArrayList of Tournament objects

I need to be able to repair the file and access the data. I've been searching for a way to read the file Tournament by Tournament saving each one into another Arraylist but I haven't been able to find anything useful.

Is this possible?

The file was saved with:

File f = new File(path);
FileOutputStream out = new FileOutputStream(f);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);
objectOutputStream.writeObject(gm.getTournamentList());
objectOutputStream.close();
out.close();

Thanks

2024-11-20 13:10:52.310 31725-31725 System.err              com...mple.herculestournamettracker  W  java.io.EOFException
2024-11-20 13:10:52.324 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2973)
2024-11-20 13:10:52.335 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.skipCustomData(ObjectInputStream.java:2140)
2024-11-20 13:10:52.346 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2114)
2024-11-20 13:10:52.357 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1935)
2024-11-20 13:10:52.365 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1442)
2024-11-20 13:10:52.370 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2186)
2024-11-20 13:10:52.376 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2110)
2024-11-20 13:10:52.382 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1935)
2024-11-20 13:10:52.386 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1442)
2024-11-20 13:10:52.390 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:430)
2024-11-20 13:10:52.394 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.util.ArrayList.readObject(ArrayList.java:988)
2024-11-20 13:10:52.396 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.lang.reflect.Method.invoke(Native Method)
2024-11-20 13:10:52.400 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1109)
2024-11-20 13:10:52.404 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2085)
2024-11-20 13:10:52.406 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1935)
2024-11-20 13:10:52.409 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1442)
2024-11-20 13:10:52.412 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2186)
2024-11-20 13:10:52.415 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2110)
2024-11-20 13:10:52.418 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1935)
2024-11-20 13:10:52.421 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1442)
2024-11-20 13:10:52.423 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:430)
2024-11-20 13:10:52.426 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.util.ArrayList.readObject(ArrayList.java:988)
2024-11-20 13:10:52.429 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.lang.reflect.Method.invoke(Native Method)
2024-11-20 13:10:52.431 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1109)
2024-11-20 13:10:52.433 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2085)
2024-11-20 13:10:52.435 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1935)
2024-11-20 13:10:52.437 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1442)
2024-11-20 13:10:52.439 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2186)
2024-11-20 13:10:52.441 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2110)
2024-11-20 13:10:52.443 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1935)
2024-11-20 13:10:52.445 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1442)
2024-11-20 13:10:52.446 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:430)
2024-11-20 13:10:52.448 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.util.ArrayList.readObject(ArrayList.java:988)
2024-11-20 13:10:52.449 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.lang.reflect.Method.invoke(Native Method)
2024-11-20 13:10:52.450 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1109)
2024-11-20 13:10:52.452 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2085)
2024-11-20 13:10:52.453 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1935)
2024-11-20 13:10:52.455 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1442)
2024-11-20 13:10:52.456 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:430)
2024-11-20 13:10:52.458 31725-31725 System.err              com...mple.herculestournamettracker  W      at com.example.herculestournamettracker.InitialImageScreen.loadAllTournaments(InitialImageScreen.java:180)
2024-11-20 13:10:52.459 31725-31725 System.err              com...mple.herculestournamettracker  W      at com.example.herculestournamettracker.InitialImageScreen.onCreate(InitialImageScreen.java:46)
2024-11-20 13:10:52.460 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.app.Activity.performCreate(Activity.java:9002)
2024-11-20 13:10:52.462 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.app.Activity.performCreate(Activity.java:8980)
2024-11-20 13:10:52.463 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1526)
2024-11-20 13:10:52.464 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4030)
2024-11-20 13:10:52.465 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4235)
2024-11-20 13:10:52.466 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:112)
2024-11-20 13:10:52.468 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.app.servertransaction.TransactionExecutor.executeNonLifecycleItem(TransactionExecutor.java:174)
2024-11-20 13:10:52.469 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.app.servertransaction.TransactionExecutor.executeTransactionItems(TransactionExecutor.java:109)
2024-11-20 13:10:52.470 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:81)
2024-11-20 13:10:52.471 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2636)
2024-11-20 13:10:52.472 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.os.Handler.dispatchMessage(Handler.java:107)
2024-11-20 13:10:52.473 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.os.Looper.loopOnce(Looper.java:232)
2024-11-20 13:10:52.474 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.os.Looper.loop(Looper.java:317)
2024-11-20 13:10:52.475 31725-31725 System.err              com...mple.herculestournamettracker  W      at android.app.ActivityThread.main(ActivityThread.java:8705)
2024-11-20 13:10:52.476 31725-31725 System.err              com...mple.herculestournamettracker  W      at java.lang.reflect.Method.invoke(Native Method)
2024-11-20 13:10:52.477 31725-31725 System.err              com...mple.herculestournamettracker  W      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
2024-11-20 13:10:52.478 31725-31725 System.err              com...mple.herculestournamettracker  W      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)

Edit: When I run this code I can see the data as a String so it appears the data is fine. I just need some similar piece of code that will go Arraylist item to Arraylist item instead of line by line.

try (BufferedReader reader = new BufferedReader(newFileReader(file.getAbsolutePath()))) {
            String line;
            while ((line = reader.readLine()) != null) {
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
6
  • Welcome to Stack Overflow. Please take the tour to learn how Stack Overflow works and read How to Ask on how to improve the quality of your question. Then edit your question to include your source code as a working minimal reproducible example, which can be compiled and tested by others to provide an answer faster. Commented Nov 20, 2024 at 21:17
  • Can you post the file contents? Commented Nov 20, 2024 at 21:58
  • @hitesh That is aggressively bad advice. Usually throws IOException is better, for example. Commented Nov 20, 2024 at 22:05
  • @vincatzero given that the file is now corrupt, fixing it requires significant expertise and weeks of full time work, and there may be nothing left to recover. Java serialization is not a nice protocol, and EOFException no doubt implies that a bunch of data has been cut off. In fact, the file is likely entirely empty, in which case, there is simply nothing to recover. And the code you wrote most definitely did not [A] complete without exceptions and [B] produced a file that now EOFs when reading it, unless said file was corrupted on disk. Commented Nov 20, 2024 at 22:06
  • 1
    re-post of Recovering a file with an java.io.EOFException :-/ Commented Nov 21, 2024 at 8:10

0

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.