RRF
RRF

Reputation: 11

Java error: getting FileNotFoundException

I´m trying to read from a file teste.txt but I only got FileNotFoundException.
(file teste.txt is located on c:\Java\teste.txt)

Here is a snippet of the code:

public class principal {
    
    public static void main(String args[]) {
       
        String test;
        
        BufferedReader in = FileIO.getReader("c:\\Java\\teste.txt");
        test = FileIO.getLine(in);
        System.out.println(test);
    }  
}

public class FileIO {
    
    public static BufferedReader getReader(String name) {
        
        BufferedReader in = null;
        
        try {
            File file = new File (name);
            String filePath = file.getAbsolutePath();
            in = new BufferedReader(new FileReader(filePath));            
        }
        catch (FileNotFoundException e) {
            System.out.println("Arquivo não encontrado");
            System.exit(0);
        }
        return in;
    }
}

Could anybody help me... Thanks

Upvotes: 1

Views: 51

Answers (1)

djangofan
djangofan

Reputation: 29689

In order to debug this, you can use a utility method called

Path path = Paths.get("does-not-exist.txt");
boolean val = Files.exists(path);

You can use built-in methods of the java.nio.file package, instead of doing that BufferedReader thing you are doing.

Upvotes: 2

Related Questions