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
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.
Obviously this is a matter of an opinion. I see your point but respectfully disagree. Trimming the white spaces is not a major modification but rather a convenience. An unintentional space at the end of any string is hard to detect and it makes it hard to understand the cause of the problem. While I would agree that excessive tolerance may harm the system (too smart of a spellcheck for instance would correct a valid person's name such as "Nati" into a "NATO" may be a case in point), too rigid rule enforcement may be harmful either. In this case, I think, tolerating trailing white spaces is closer to a "golden" middle ground. But it is of course like I said a matter of opinion. Regardless, since it was an unexpected behavior to me, I figured this note might be useful to others (whether they agree or disagree with a sentiment)