Naldery
Naldery

Reputation: 13

Why does this throw java.nio.file.InvalidPathException?

The program throws

 Exception in thread "main" java.nio.file.InvalidPathException: Illegal char 
  <:> at index 2: ‪E:\New folder
      at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
      at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
      at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
      at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
      at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
      at java.nio.file.Paths.get(Paths.java:84)
      at 
 javaapplication10.ACDirectoryListenerServiceImpl.main(ACDirectoryListenerServiceImpl.java:93)
Java Result: 1

When I try to get a path of a file with

Path path = Paths.get("‪E:\\New folder");

why is that? Are drive letters allowed when getting paths?

Upvotes: 0

Views: 2758

Answers (1)

Matthias Ronge
Matthias Ronge

Reputation: 10102

The „:“ is „at index 2“, that is the third character of the string. The drive letter should be at index 0, and the „:“ should be at index 1.

There is a zero-width character in your string. You can find the character by moving the cursor letter by letter with the arrow keys. The character is where the cursor seems not to react to the keypress.

I see this happening when I copy a file path from Windows’ “file properties” dialog, security page, „object name“, when I select the path with the mouse from end to beginning, there is a non-printing unicode “left-to-right embedding” character (U+202A) before the drive letter. It is not visible even with enabled (I tried Notepad++ and Eclipse). This does not happen when I select the path from beginning to end.

Credit to Slaw who pointed in the right direction in his comment.

Upvotes: 1

Related Questions