javafileeofexception

End of File Exception on ObjectInputStream.readObject


My application streams twitter data and writes them to files.

while(true){
        Status status = queue.poll();

        if (status == null) {
            Thread.sleep(100);
        }

        if(status!=null){
            list.add(status);
        }

        if(list.size()==10){
            FileOutputStream fos = null;
            ObjectOutputStream out = null;
            try {
                String uuid = UUID.randomUUID().toString();
                String filename = "C:/path/"+topic+"-"+uuid+".ser";
                fos = new FileOutputStream(filename);
                out = new ObjectOutputStream(fos);
                out.writeObject(list);
                tweetsDownloaded += list.size();
                if(tweetsDownloaded % 100==0)
                    System.out.println(tweetsDownloaded+" tweets downloaded");
            //  System.out.println("File: "+filename+" written.");
                out.close();
            } catch (IOException e) {

                e.printStackTrace();
            }

            list.clear();
    }

I have this code which gets data from files.

while(true){
    File[] files = folder.listFiles();

    if(files != null){
        Arrays.sort(//sorting...);

        //Here we manage each single file, from data-load until the deletion
        for(int i = 0; i<files.length; i++){
            loadTweets(files[i].getAbsolutePath());
            //TODO manageStatuses
            files[i].delete();
            statusList.clear();
        }

    }

}

The method loadTweets() does the following operations:

private static void loadTweets(String filename) {

    FileInputStream fis = null;
    ObjectInputStream in = null;
    try{
        fis = new FileInputStream(filename);
        in = new ObjectInputStream(fis);
        statusList = (List<Status>) in.readObject();
        in.close();
    }
    catch(IOException | ClassNotFoundException ex){
        ex.printStackTrace();
    }


}

Unfortunately, I don't know why sometimes it throws a

EOFException

when running this line

statusList = (List<Status>) in.readObject();

Anybody knows how I can solve this? Thank you.


Solution

  • Found out what was necessary to solve this. Thanks to @VGR's comment, I thought to pause the executing thread for 0.2 seconds if the file has been created less than a second ago.

    if(System.currentTimeMillis()-files[i].lastModified()<1000){
            Thread.sleep(200);
    

    This prevents the exception and the application works now fine.