Solved

Getting an error "Bad File Descriptor" when trying to open a file

Posted on 2008-10-01
16
297 Views
Last Modified: 2012-05-05
Experts,

         I am getting an error in JAVA when I try to run my program. It compiles fine, and the program does run but I get an error message right before the first instruction "Bad File Descriptor". I posted my code below:

How do I resolve this error?
public void ReadSMLFile(String Filename) {

    // Open the file that is the first

    // command line parameter

    try {

      FileInputStream fstream = new FileInputStream(Filename);

      DataInputStream in = new DataInputStream(fstream);

      BufferedReader br = new BufferedReader(new InputStreamReader(in));

      String strLine;
 

      //Read file line by line

      while ( (strLine = br.readLine()) != null) {

        //if -99999 is found indicates end of file

        if (strLine.equals("-99999"))

          break;
 
 

AND the Main Function:
 

public static void main(String args[]) {

    try {

      Simpletron s = new Simpletron();

      if (args.length != 1)

        System.out.println("Must Provide Filename as Arguments");

      else

      {

        s.ReadSMLFile(args[0]);

        s.CodeProcess();

      }

    }

    catch (Exception e) {

      //System.out.println("File not found");

      System.err.println("Error: " + e.getMessage());

Open in new window

0
Comment
Question by:missymadi
  • 9
  • 7
16 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22617853
What is the file name that you are passing it? Does this file exist?
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22617874
What type of Error was thrown? FileInputStream throws errors in these cases:

"Throws:
FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
SecurityException - if a security manager exists and its checkRead method denies read access to the file."
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22617917
This thread also listed a Bad File Descriptor issue:

http://www.experts-exchange.com/Programming/Languages/Java/Q_22838235.html
0
 

Author Comment

by:missymadi
ID: 22617934
Yes, the file exists. The file is called TestCase1.txt. What is strange is that I created a test file called "test.txt" and ran it.  It shows the error: bad file descriptor then runs the program???
0
 

Author Comment

by:missymadi
ID: 22617959
Yes, I saw the post that you have a link to, but that doesn't seem to be the issue in my case.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22617969
Could you post the stack trace please?
0
 

Author Comment

by:missymadi
ID: 22617990
Where is that located?
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22618019
When you get this error popup on the screen, goto the window hit "ctrl + c" and then paste it here.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:missymadi
ID: 22618099
This program is run from the command prompt. The entry is as follows:

c:\java Filename test.txt
Error: Bad file descriptor
***Program execution terminated***     This is the code running correctly that I put in the file. So it is running but getting a weird message in the beginning of the program execution.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22618202
Before we continue we first need to get this stacktrace

In your main method (the top most level)

wrap everything in a try/catch block like this:

public static void main(...){
   try
   {
      ... your code here ...
   }
   catch(Exception e){
      e.printStackTrace();
      System.out.println();
      System.out.println(e.getMessage());
   }
}
0
 

Author Comment

by:missymadi
ID: 22618371
OK. compiled and executed successfully.
0
 

Author Comment

by:missymadi
ID: 22618634
Where do I find the stack trace??
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22618668
Hm interesting. So it still outputted the same thing with no stack trace in the output?
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22618701
Could you do this for me, change the catch block to this:

      System.out.println("Here is the Stack Trace: ");
      e.printStackTrace();
      System.out.println();
      System.out.println(e.getMessage());


verify that it does indeed say "Here is the Stack Trace:". I am uncertain if even this catch block is called to begin with. To be it seems as if the code ran, and somewhere else inside the code it prints out the error message... but not here.
0
 

Author Comment

by:missymadi
ID: 22623014
I added the code you suggested. Then I compiled and ran the project.

C:\Javastuff\file.java
Error!: No such file or directory
Enter a number:

I found the code where Error!: No such file or directory.....is running..I'll attach it below. It seems this line of code runs first then my program runs as instructed. How do I fix this?
public void ReadSMLFile(String Filename) {

    // Open the file that is the first

    // command line parameter

    try {

      FileInputStream fstream = new FileInputStream(Filename);

      DataInputStream in = new DataInputStream(fstream);

      BufferedReader br = new BufferedReader(new InputStreamReader(in));

      String strLine;
 

      //Read file line by line

      while ( (strLine = br.readLine()) != null) {

        //if -99999 is found indicates end of file

        if (strLine.equals("-99999"))

          break;
 

        //store line read into array sub i and remove the '+' char at the front

        opcodes[i] = Integer.parseInt(strLine.substring(1));
 

        //increment array index for next read

        i++;
 

        //Print the content on the console

        //System.out.println (strLine);
 

        in.close();

      }

    }

    catch (Exception e) { //FileNotFoundException e)

      //System.out.println("file not found");

      System.err.println("Error!: " + e.getMessage()); ****right here!!

    }

  }

Open in new window

0
 
LVL 6

Accepted Solution

by:
RishadanPort earned 125 total points
ID: 22625891
Do this:

at the catch block type in this:

    catch (Exception e) { //FileNotFoundException e)
      System.out.println("file not found");
      System.err.println("Error! Problem with file: " + Filename); ****right here!!
    }

once it prints the path to the file, make sure that that file does indeed exist.

One thing important to understand is that the program, after the catch block is executed continues on.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now