Hello,
I am writing a method which must open a file, read its contents, close the file, open it later, write to the file, and close it again. The method is failing at the first step and throwing an IOException when trying to execute:

FileInput Stream fileHandler = new FileInputStream("myfile.fil");

I am somewhat baffled as to why this is occurring. The file is in the proper directory, it is not in use, and the other handlers to it are closed before this call is made to it. Any ideas as to what is going on? Thanks!

I hope that is supposed to be:

FileInputStream fileHandler = new FileInputStream("myfile.fil");

furthermore, that constructor should not be throwing any IOExceptions, do you have more code or the rest of the exception stack trace?

It's throwing an EOFException when attempting to open the file. The initial contents of the file is only an int, specifically '0'. I'm not really sure what to make of it. I tested the code with debug statements and this is definitely the code causing the problem. All other streams to this file are terminated prior to the call to this line.

Try opening it with this constructor:

public FileInputStream(File file);

First, create the File Object. See if creating the File object also throws an Exception; if so, print the Exception's contents and getMessage() message in here. Also use the File class's file.canRead() method and tell me if it returns true or false. These are just some thoughts - there's no harm in using a different class to try to find out information about the problem, although this may not yield any answers.

>It's throwing an EOFException when attempting to open the file.

No, that's not true. FileInputStream constructor throws FileNotFoundException.

EOFException signals that an end of file or end of stream has been reached unexpectedly during input.

Post your complete code.

The exception stack trace said it was throwing an EOFException. Thanks JC I'll have to try that. I know its very weird error to have when opening a file but that's what it's telling me. I'll try to post the entire peice of code tonight.

This question has already been answered. Start a new discussion instead.