dcsimg
Bad File Number IOException when writing to disk
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   James_Richards
Posted On:   Tuesday, September 3, 2002 09:48 AM

I am writing byte arrays to files on disk using my own store method. I use a DataOutputStream to write to an underlying ByteArrayOutputStream. Once I have collected the whole stream I use the writeTo method of the ByteArrayOutputStream to write these bytes to a FileOutputStream in append mode as follows // .. Create streams to write bytes baos = new ByteArrayOutputStream(); dos = new DataOutputStream(baos); // .. write byte stream .. // Create stream to write bytes to file FileOutputStream os = new FileOutputStream(pathName, true); // write bytes to file baos.writeTo(os); I obviously place try catches around the appropriate areas   More>>


I am writing byte arrays to files on disk using my own store method. I use a DataOutputStream to write to an underlying ByteArrayOutputStream. Once I have collected the whole stream I use the writeTo method of the ByteArrayOutputStream to write these bytes to a FileOutputStream in append mode as follows


			
// .. Create streams to write bytes
baos = new ByteArrayOutputStream();
dos = new DataOutputStream(baos);
// .. write byte stream ..
// Create stream to write bytes to file
FileOutputStream os = new FileOutputStream(pathName, true);
// write bytes to file
baos.writeTo(os);


I obviously place try catches around the appropriate areas and in a finally clause I close all of the streams using close methods


I run my store method about 12,000 times during the course of my entire program. The method is synchronised and no file should be accessed concurrently.


The problem is that I am getting an IOException "Bad File Number" being thrown on average 12 times out of these 12,000 and this is causing problems when reading the files in a subsequent process.


What could be causing these errors? Have I written unstable code and what would be better? If not, then how can I write my code to trap this problem before using the baos.writeTo(os) method above?

   <<Less

Re: Bad File Number IOException when writing to disk

Posted By:   Christopher_Koenigsberg  
Posted On:   Tuesday, September 3, 2002 12:13 PM

Have I written unstable code



Sure sounds like it, if you are getting "Bad File Number" exceptions!



I think (fuzzy memories from the old days programming Unix in C) that the "File Number" in the error refers to the Unix style file descriptors e.g. "0" for stdin, "1" for stdout, "2" for stderr, and so on if you open more of your own.



I think the error can come if you are trying to write to a file descriptor which you previously opened, but have already closed? Or failed to open yet? Or a bad "pathName", so the open failed, etc. etc.

About | Sitemap | Contact