I am in a refactoring stage for a project I am working on and would like to make some improvements to how I build and represent file system paths. What things should I take into consideration when representing relative paths in Java code to ensure compatibility on Ubuntu, OSX, and Windows 7.
Currently to get an instance of File referencing “MyProject/foo/bar.f” I would have code along the lines of:
File bar = new File(ProjectDirectory + "/" + FooResourceDirectory + "/" + barName);
This seems wrong for several reasons, what are some of the best practices?
Perhaps use the constructors provided to do this sort of thing:
You have to “nest” them, but it’s trivial to handle this (e.g. make a function to get a path built from something taking
string....)See the File constructors.