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:
new File(parent, child)
You have to "nest" them, but it's trivial to handle this (eg make a function to get a path built from something taking string...
.)
See the File constructors.
首先,您应该使用File.separator
而不是“/”。 File.pathSeparator
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.