943,685 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 698
  • Java RSS
Jun 23rd, 2009
0

IOException when opening FileInputStream

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  1. 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!
Similar Threads
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Jun 23rd, 2009
0

Re: IOException when opening FileInputStream

I hope that is supposed to be:

java Syntax (Toggle Plain Text)
  1. 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?
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Jun 24th, 2009
0

Re: IOException when opening FileInputStream

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.
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Jun 24th, 2009
0

Re: IOException when opening FileInputStream

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.
Last edited by BestJewSinceJC; Jun 24th, 2009 at 1:21 am.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jun 24th, 2009
0

Re: IOException when opening FileInputStream

>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.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 24th, 2009
0

Re: IOException when opening FileInputStream

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.
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Loading from text file help.
Next Thread in Java Forum Timeline: JOptionPane hide-and-seek





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC