I'm trying to read input from a file to be taken into a Java applet to be displayed as a Pac-man level, but I need to use something similar to getLine()... So I searched for something similar, and this is the code I found:

File inFile = new File("textfile.txt");
FileInputStream fstream = new FileInputStream(inFile);//ERROR
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

The line I marked "ERROR" gives me an error that says "Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor."

I've searched for this error message, but everything I find seems to be unrelated to my situation.

share|improve this question
1  
Can you show more of your code, we don't know the context of what you have posted above. – jbranchaud Feb 20 '12 at 0:36
    
    
You didn't even quote the error message accurately. It does tell you exactly what the problem is, but not if you don't read what's actually there. – EJP Nov 29 '16 at 23:50

Either declare a explicit constructor at your subclass that throws FileNotFoundException:

public MySubClass() throws FileNotFoundException {
} 

Or surround the code in your base class with a try-catch block instead of throwing a FileNotFoundException exception:

public MyBaseClass()  {
    FileInputStream fstream = null;
    try {
        File inFile = new File("textfile.txt");
        fstream = new FileInputStream(inFile);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        // Do something with the stream
    } catch (FileNotFoundException ex) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            // If you don't need the stream open after the constructor
            // else, remove that block but don't forget to close the 
            // stream after you are done with it
            fstream.close();
        } catch (IOException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
    }  
} 

Unrelated, but since you are coding a Java applet, remember that you will need to sign it in order to perform IO operations.

share|improve this answer

You need to surround your code with try and catch as follows:

try {
    File inFile = new File("textfile.txt");
    FileInputStream fstream = new FileInputStream(inFile);//ERROR
} catch (FileNotFoundException fe){
    fe.printStackTrace();
}
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
share|improve this answer
    
Adel, fstream is not visible to the DataInputStream constructor if you write your code like this. You need to refactor your code and have a null referenced fstream outside of the try block like I did in my example. Also, since you are not surrounding the entire block with a try / catch statement, it is not a bad idea to check for fstream != null before declaring the in and br variables. – Anthony Accioly Feb 20 '12 at 1:03
    
@AnthonyAccioly I didn't present a complete solution, I just gave you a clue, the try and catch should envelop all lines which can produce the mentioned error. Sadly, I didn't check which lines do that. It is up to you to use it at your convenience :) – Adel Boutros Feb 20 '12 at 1:40

This is guesswork as we don't have the complete code.

From the Javadoc: public FileInputStream(File file) throws FileNotFoundException

It means that when you do a new FileInputStream() like you do, it can come back with a FileNotFoundException. This is a checked exception, that you need to either rethrow (i.e. add 'throws FileNotFoundException' in the method where you do the new) or catch (see other try/catch responses).

share|improve this answer

I would say that the textfile.txt does not exist or is not in the current directory.

So you are trying to create the FileInputStream on a file that does not exist.

try doing a

System.out.println(inFile.getAbsolutePath() + " " + inFile.exists());

on the file and make sure that it does exist before you try and use it.

share|improve this answer
2  
I think the issue is the compiler error (I suspect the code above is pasted in a constructor) - not a runtime 'file doesn't exist' error. – ptyx Feb 20 '12 at 0:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.