-1

This is what I am trying below on JUnit test class.

String imagePath  = "/⁨Users⁩/⁨mymacbook/⁨Desktop⁩/⁨Web_Projects⁩/books⁩/EffectiveJava.jpg";
byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));

This is in the test class but when I run the test class I get the below error.

java.nio.file.NoSuchFileException: /⁨Users/⁩mymacbook/⁩⁨Desktop/⁩⁨Web_Projects/⁩books⁩/EffectiveJava.jpg

The above path is external to Java project.

Any help would be greatly appreciated.

prady
  • 1
  • 2
  • Is the file actually there? – Federico klez Culloca Oct 07 '19 at 15:30
  • Yes I have pasted the file there and checked it as well manually – prady Oct 07 '19 at 15:31
  • 1
    Have you checked that the user you use to launch the java class has permission to read the file? – Federico klez Culloca Oct 07 '19 at 15:32
  • 5
    You've got various extra non-printable characters throughout that path. For example, instead of "`/Users`, there's an extra non-printable character in between the `/` and the `U`. This happens for each of the `/` characters in your path. I found this by pasting your code snip into vim. If you cut+paste that full path into a Terminal window, what does `ls` show you? – Kaan Oct 07 '19 at 15:35
  • 2
    Yeah, what @kaan tells: it is full of "FIRST STRONG ISOLATE" (8296) and "POP DIRECTIONAL ISOLATE" (8297) code points (whatever they are, from http://www.unicode.org/charts/PDF/U2000.pdf - 2068 and 9 in hexa). Your native language might have right-to-left text, I guess. – tevemadar Oct 07 '19 at 15:49
  • @kaan is right, there are bidirectional control characters FSI and PDI in the string. `imagePath = imagePath.replaceAll("[\u2068\u2069]", "");` – Joop Eggen Oct 07 '19 at 15:49
  • Thank you all, This was really helpfull. I did copy the path into sublime text and then used it in eclipse, then also was getting the error. How do I know if this unseen characters are present!?. I really dont want to use "replaceAll" all the time!? – prady Oct 08 '19 at 12:42

1 Answers1

1

May I suggest you use a system property and Path

String home = System.getProperty("user.home");
Path path = Paths.get(home, "Desktop", "Web_Projects⁩", "book", "EffectiveJava.jpg");

home will contain your home folder and path the full path with the correct separator for your OS

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52