CJR
CJR

Reputation: 3572

Can't find .txt file?

I can't find the .txt file that I want to read from.

Here is the code:

public void read(){

    try {
        File file = new File("las");
        String path = file.getAbsolutePath();
        System.out.println(path);
        BufferedReader reader = new BufferedReader(new FileReader("/Users/Asus/workspace/testhitodit/las.txt"));
        String line = reader.readLine();
        while(line != null) {
            System.out.println(line);
            line = reader.readLine();
            }
        reader.close();
    }catch(FileNotFoundException e) {
    System.out.println("File not found");
    }catch(IOException e) {
    System.out.println("SOmething went wrong");
    }
}

This is what gets printed out:

C:\Users\Asus\workspace\testhitodit\las

File not found

And I have a file named las.txt in that folder.

Why doesn't it work?

Upvotes: 0

Views: 1107

Answers (1)

terrywb
terrywb

Reputation: 3956

   //include the file extension
   File file = new File("las.txt");
   String path = file.getAbsolutePath();
   System.out.println(path);

   //pass the file you created to your file reader
   BufferedReader reader = new BufferedReader(new FileReader(file));

Upvotes: 1

Related Questions