IOException when opening FileInputStream

Reply

Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

IOException when opening FileInputStream

 
0
  #1
Jun 23rd, 2009
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:
  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!
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: IOException when opening FileInputStream

 
0
  #2
Jun 23rd, 2009
I hope that is supposed to be:

  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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: IOException when opening FileInputStream

 
0
  #3
Jun 24th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: IOException when opening FileInputStream

 
0
  #4
Jun 24th, 2009
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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: IOException when opening FileInputStream

 
0
  #5
Jun 24th, 2009
>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.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: IOException when opening FileInputStream

 
0
  #6
Jun 24th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC