Java – Cross Platform file path construction and representation
•
Java
I'm in the refactoring phase of my ongoing project and want to make some improvements on how I build and represent file system paths What should I consider when representing relative paths in Java code to ensure the compatibility of Ubuntu, OSX and windows 7
Currently, to get an instance of the file reference "myproject / foo / bar. F", I will use the following code:
File column = new file (projectdirectory "/" fooresourcedirectory "/" barname);
This seems wrong for several reasons. What are the best practices?
Solution
You may use the provided constructor to perform such operations:
new File(parent,child)
You have to "nest" them, but it's easy to deal with this problem (for example, create a function to get a path constructed from a string...)
See file constructors
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码