2

I'm getting eofexception I tried to solve it but didn't find an answer it is giving exception in ObjectInputStream line please help me out.

public class SearchIndex extends ObjectOutputStream {
  static Map<String, List<String>> map = new HashMap<String, List<String>>();

  public SearchIndex(OutputStream out) throws IOException {
    super(out);
 }

  public static void readIndex() throws FileNotFoundException, IOException, ClassNotFoundException {

     // create an ObjectInputStream for the file we created before
    File file = new File("res/searchIndex.txt");
    FileInputStream fis = new FileInputStream(file);
    ObjectInputStream ois = new ObjectInputStream(fis);

    System.out.println(ois.readObject());
    // read and print an int
    map =  (Map<String, List<String>>) ois.readObject();
    System.out.println(map);
    ois.close();
  }

help me solve this problem

1
  • 1
    Answer: When the input file is empty the FileInputStream will return -1 as output so, check if (fis != -1 ) then go further... because of FileInputStream returning me -1 the EOF exception is occurring .... hope this will help you. Commented Oct 16, 2015 at 16:45

3 Answers 3

3

Its an expected behavior, ois.readObject() reached end of the file and couldnt fine anything further. Handle it inside a try catch block

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, but I'm getting exception at creating ObjectInputStream object;
1

The error is only because your file at location res/searchIndex.txt is empty.

Check if you have any data in your file..

Moreover, since you used ObjectInputStream, your text content won't get converted automatically into bytes. If you are using readObject() for reading; then you have to use writeObject() for writing contents into file.

3 Comments

Thank you for reply, Yes, my file is empty. I got over the effect by checking the readInt() is -1 or not. As you said , I used readObject() and writeObject() but the writeObject is writing some dump into the file. thanks in advance
I am developting simple search index; sample file entries look like this: keyword: url1, url2, url3; keyword1: url2, url4; help me @Ashish
requesting you to open new question if you required any other assistance . also if your above exception is resolved ; can you accept it and close this question?
-1

You need to explicitly cast the object to String.

Use

 System.out.println((String) ois.readObject());

And it should work.

5 Comments

Thanks for the answer, the exception is not printing the object, but I'm getting exception at creating ObjectInputStream object;
@sreenathsirimala okay... where and how are you using the writeObject method in your code? can you please post it here?
I have removed entire logic and written on my own @megh vidani thanks for reply
If you have solved the issue, It would be great if you could share your own way of solving it so that it can help someone in the future. :)
I have added the answer in a comment :-) thanks for suggestion .@megh

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.