washing_dishes
washing_dishes

Reputation: 1

System cannot find file specified Java

I understand this a basic problem and I am most likely looking right past the solution, but I do not see where I am going wrong. I've looked at other answers and have not received anything that seems to help.

try {
            FileReader reader = new FileReader("C:\\Users\\ethan\\Desktop\\MyFile.txt");
            int character;

            while ((character = reader.read()) != -1) {
                System.out.print((char) character);
            }
            reader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

Upvotes: 0

Views: 658

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109532

Maybe the file actually is MyFile.txt.txt (with hidden extension). The following utility helps finding the actually part wrong: containing directory or file name.

Path path = Paths.get("C:\\Users\\ethan\\Desktop\\MyFile.txt");
checkPath(path);

boolean checkPath(Path path) {
    if (!Files.exist(path) {
        Path parent = path.getParent();
        if (parent != null && checkPath(parent)) {
            String name = path.getFileName().toString();
            System.out.printf(
                "In directory %s the following name is not found: '%s' = %s%n.",
                parent.toString(), name,
                Arrays.toString(name.toCharArray()));
        }
        return false;
    }
    return true;
}

Upvotes: 2

Related Questions