Java 7 introduced a nio (new io) package with tons of exciting features and improvements. But recently I ran into some small feature (I am reluctant to call it a bug). Class java.nio.filePaths is a convenience class that allows to convert a
String (or URI) into an instance of class Path which is one of the major components of nio. Method
Paths.get() does not tolerate trailing white spaces. So lets say if a value passed to the method would be "C:\\user\\temp" it will work fine. but should it become "C:\\user\\temp " (note a space at the end of the string), it will cause the exception to be thrown:
"Exception in
thread "main" java.nio.file.InvalidPathException: Trailing char > at index 12: C:\user\temp"
And that was a surprise to me. In our project we read some path for a an output folder from an external configuration file that is meant for a user to configure. we use method Paths.get() to evaluate if the path exists
Files.exists(Paths.get(userProvidedPath).
So, should user make a simple typo by adding a space at the end of a path the program would crash. Obviously the simple fix of using method userProvidedPath.trim() on the String before passing it to method Paths.get() fixes the problem. But I was expecting the code that is part of java language to be tolerant to trailing white spaces for the path since paths that only differ by trailing white spaces should be considered equal. Just to note - the problem was observed on Windows. I don't know if it reproduces on Unix/Linux.