0

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.

7
  • 1
    EOFException suggests that there is no more data to read. Based on your code it looks like it happens at start of the file, which means it is empty. Why it is empty at the point of reading? Possibly because you didn't add anything to it yet (different thread erased its data, and before managed to add new one your thread attempted to read file content). It is hard to tell without having minimal reproducible example. Commented Dec 5, 2017 at 18:06
  • @Pshemo The file is not empty, in fact its length is greater than zero when this happens. How could I handle this? Commented Dec 5, 2017 at 18:56
  • Edit your question and show the code which was used to write those files. Commented Dec 5, 2017 at 19:17
  • 1
    Have you taken any steps to ensure that your file reading code does not execute at the same time your other code is writing the same file? Commented Dec 5, 2017 at 19:33
  • 1
    "As far as I know the file is available to the read whenever its write has ended" that may be true, if you are in the middle of writing to file then others can't read from it, but that is not the case here. Your file is being "reset" by new FileOutputStream(filename);. After that it doesn't have any content, but before you execute out.writeObject(list); which may lock the file (depending on buffering) there is some time period where file is not being used but is empty which seems to be causing your problem. This looks like classic readers-writers problem. You need synchronization. Commented Dec 6, 2017 at 16:26

2 Answers 2

2

I've seen that you're passing the file correctly with the getAbsolutePath() based on a previous question of yours

From what I've read that can be a couple of things, one of them the file being null.

Explaining this idea, you might have written the file but something caused the file to have nothing inside, this might cause an EOFException. The file in fact exists it's just empty

EDIT

Try to enclose the code in while(in.available() > 0)

It would look like this

private static void loadTweets(String filename) {

    FileInputStream fis = null;
    ObjectInputStream in = null;
    try{
        fis = new FileInputStream(filename);
        in = new ObjectInputStream(fis);
        while(in.available() > 0) {
            statusList = (List<Status>) in.readObject();
        }
        in.close();
    }
    catch(IOException | ClassNotFoundException ex){
        ex.printStackTrace();
    }
}
Sign up to request clarification or add additional context in comments.

14 Comments

So you would suggest me to ignore the file and simply go on?
Handle the error and keep going, just knowing that you have nothing on that file so the status list will be in fact null
Thank you, I'll follow your suggestion.
Unfortunately this keeps throwing the error and I verified that the file was not null, by printing File.length. The error it prompts are "peekByte" and "readFully". Do you know how to handle this?
Hum... can you print the stack trace
|
1

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.

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.