Sunday, January 10, 2016

File.getName() in windows and Unix

If you have a full path like filename="c:\temp\pippo.txt" , on windows new File(filename).getName() will return pippo.txt, but on *nix it will return "c:\temp\pippo.txt".

This can be a nasty issue: you want a path to be parsed consistently regardless of the OS on which the JVM is running. Well, that's not the case. So you are screwed.

Luckily you can use https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FilenameUtils.html and it will work beautifully.

No comments: