Java – Cross Platform file path construction and representation

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
分享
二维码
< <上一篇
下一篇>>