0

I have tried this a hundred different ways but keep getting a filenotfound... Put it as simple as I could and don't understand it.

File file = new File("C:/files/salesrep.txt");
FileReader fileSC = new FileReader(file);
BufferedReader br = new BufferedReader(fileSC);

The FileReader is giving me a file not found. I tried using the directory and get the same.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    The simplest explanation is that the file isn't in that location and your path is just wrong. You may want to check in Java where you're actually looking. I'm also somewhat confident in this answer because in most cases, you can't (or at least shouldn't) be putting files in the root `C` folder anyway, or in non-default subfolders thereof like `files`. – ifly6 Jun 08 '18 at 16:28
  • try, System.out.println(file.exists()); – bit-shashank Jun 08 '18 at 16:29
  • The code looks correct. Either you have the wrong path, or don't have permissions to access the file. – Mureinik Jun 08 '18 at 16:29
  • 1
    It _might_ be because you're using the UNIX directory separators instead of the Windows ones; use `File.pathSeparator` instead of `/` ("C:" + File.pathSeparator + "files" + File.pathSeparator + "salesrep.txt") – Chris Forrence Jun 08 '18 at 16:31
  • 1
    Another thought, you might want to check the file name. If you have file extensions turned off in Windows, the file name might actually be salesrep.txt.txt – Chris Parker Jun 08 '18 at 16:33
  • You’ll get a much less ambiguous exception if you use [Files.newBufferedReader](https://docs.oracle.com/javase/10/docs/api/java/nio/file/Files.html#newBufferedReader(java.nio.file.Path)): `BufferedReader br = Files.newBufferedReader(Paths.get("C:/files/salesrep.txt"));` – VGR Jun 08 '18 at 16:34
  • Are you **certain** the case is correct? I.E. not `"C:/Files/salesrep.txt"`or `"C:/files/SalesRep.txt"` or `"C:/files/salesrep.TXT"`? – Andrew Thompson Jun 09 '18 at 02:59

1 Answers1

0

Try using path like this, if the file exists the shouldn't be a problem.

File file = new File("C:\\files\\salesrep.txt");

Also you can here for more specific explanation.

namokarm
  • 668
  • 1
  • 7
  • 20
  • 1
    I use forward slashes instead of back slashes all the time. What you suggest is possibly the answer, but not likely. In fact, you can even use forward slashes in many Windows only programs that have nothing to do with Java. – Chris Parker Jun 08 '18 at 16:34
  • I'm used to reading from files like this, it has always worked for me... Also check the link in my edit where you can find a some usefull links for reading plain text files (assuming on the *.txt). – namokarm Jun 08 '18 at 16:37
  • 2
    I guess I didn't say that very well. Back slashes will certainly work in Windows. It's also certainly possible that what you suggest is in fact the problem (although I would find that surprising). What I was trying to say is that it's more likely some other issue, considering that I use forward slashes all the time. Your suggestion for more information is excellent though. It's definitely worth the O.P.s time to read the answer in that link. – Chris Parker Jun 08 '18 at 16:44
  • That didn't work either and the file exist. I am about to start reading the link. – Jacob McCall Jun 08 '18 at 17:59