Reputation: 6968
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?
Upvotes: 8
Views: 3713
Reputation: 120791
First of all you should use File.separator
instead of "/".File.pathSeparator
Upvotes: 9
Reputation:
Perhaps use the constructors provided to do this sort of thing:
new File(parent, child)
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.
Upvotes: 12