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();
        }
  • What exactly is the problem? I assume you get a stacktrace, right? If so you should post it. Also please read [ask] while you're at it. – Thomas Oct 16 '19 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 – washing_dishes Oct 16 '19 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? – Thomas Oct 16 '19 at 14:46
  • @Thomas The text file is sitting on my desktop, I ran Eclipse as both Admin and non-elevated user to no avail. – washing_dishes Oct 16 '19 at 14:47
  • 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` – Tom Oct 16 '19 at 14:49
  • The code works fine for me if I use my own username with `MyFile.txt` on my desktop. – Ivar Oct 16 '19 at 14:50
  • I checked the properties of the .txt and copied the location. I'm thinking you might be right on the .txt.txt, I'll check. @Thomas – washing_dishes Oct 16 '19 at 14:50
  • @Thomas you were right, I was pulling hair trying to figure this out. – washing_dishes Oct 16 '19 at 14:52
  • 1
    Possible duplicate of [The system cannot find the file specified in java](https://stackoverflow.com/questions/11553042/the-system-cannot-find-the-file-specified-in-java) (specifically [this answer](https://stackoverflow.com/a/21478067)) – Ivar Oct 16 '19 at 14:55

1 Answers1

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;
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 1
    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). – Gene Oct 16 '19 at 14:59
  • This was the problem. I'm going to look into disabling that feature, I did not know you could @Gene – washing_dishes Oct 16 '19 at 15:09