0

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();
        }
9
  • What exactly is the problem? I assume you get a stacktrace, right? If so you should post it. Also please read How to Ask while you're at it. Commented Oct 16, 2019 at 14:43
  • Thought the title was self-explanatory, but I am getting C:\Users\ethan\Desktop\MyFile.txt (The system cannot find the file specified) @Thomas Commented Oct 16, 2019 at 14:44
  • 1
    Well, are you sure that file exists and that the user you're using to run the application is allowed to access it? Commented Oct 16, 2019 at 14:46
  • 2
    That file being on your desktop doesn't mean that your desktop is still under C:\\Users\\ethan\\Desktop and I wonder if the file name is really MyFile.txt and not MyFile.txt.txt Commented Oct 16, 2019 at 14:49
  • 1
    Possible duplicate of The system cannot find the file specified in java (specifically this answer) Commented Oct 16, 2019 at 14:55

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's a great guess. First year CS students I taught often ran into this until we had them disable suffix elision in Explorer (and Finder for MacOS).
This was the problem. I'm going to look into disabling that feature, I did not know you could @Gene

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.