This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem reading file: Bad file descriptor

 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a servlet running under Tomcat 5.0.28 which reads data from a large binary file on disk. Problem is I am seeing a java.io.IOException: Bad file descriptor when I try to use RandomAccessFile.read() call.



I'm at a bti of a loss on this one. A stand alone app which does the same thing on the same PC reading from the same data file accesses it no problem. I'd be grateful of any suggestions, especially from anyone who has seen this error before under Tomcat. I have experienced something similar before but I cannot remember how it was resolved

cheers,
Ben
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you locating the file on the filesystem?
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file locations are being read from a database table, some are in the form:

E:\aDirectory\aFile.dat

Others are network paths like:

\\networkStore\\aDirectory\\aFile.dat

Neither seem to work, although they do work when used with a stand-alone app.
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turned out to be a premature close() call on RandomAccessFile.
 
John Te
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I also run the same problem today.

Platform : MAC OS with Java 1.6, Tomcat 6.x --

Purpose: Try to create a new file and write new file content inside Tomcat servlet.

Exception in Tomcat:


java.io.IOException: Bad file descriptor
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:260)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
at sun.nio.cs.StreamEncoder.implClose(StreamEncoder.java:297)
at sun.nio.cs.StreamEncoder.close(StreamEncoder.java:130)
at java.io.OutputStreamWriter.close(OutputStreamWriter.java:216)
at java.io.BufferedWriter.close(BufferedWriter.java:248)


Code:
// write to the same file
File fh2 = null;
try {
fh = resource.getFile();
fh2 = new File(fh.getAbsolutePath() + ".new");
os = new FileOutputStream(fh2);
bw = new BufferedWriter(new OutputStreamWriter(os));
for (int i = 0; i < list.size(); i++) {
bw.write(list.get(i));
System.out.println(list.get(i));
}
} catch (Exception e) {
throw e;
} finally {
try {
if (os != null) {
os.close();
}



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic