Java 中的平台独立路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3548775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:45:35  来源:igfitidea点击:

Platform independent paths in Java

javafilecross-platform

提问by jakewins

I know the relative path of a file and want to handle it as a Fileobject on both Linux and Windows.

我知道文件的相对路径,并希望File在 Linux 和 Windows 上将其作为对象进行处理。

What is the best way to specify platform-independent paths in Java?

在 Java 中指定与平台无关的路径的最佳方法是什么?

采纳答案by jjnguy

The Fileclass contains the following public members that you can use for platform specific file paths:

File类包含可用于特定平台的文件路径下面的公共成员:

static String pathSeparator:
The system-dependent path-separator character, represented as a string for convenience.
static char pathSeparatorChar:
The system-dependent path-separator character.
static String separator:
The system-dependent default name-separator character, represented as a string for convenience. static char separatorChar:
The system-dependent default name-separator character.

static String pathSeparator:
系统相关的路径分隔符,为方便起见表示为字符串。
static char pathSeparatorChar:
系统相关的路径分隔符。
static String separator:
系统相关的默认名称分隔符,为方便起见表示为字符串。 static char separatorChar:
系统相关的默认名称分隔符。

回答by Steven Ourada

Java is pretty smart about paths in File objects. I just use something like "../foo/bar" and it works in those two platforms plus MacOSX.

Java 对 File 对象中的路径非常聪明。我只是使用类似“../foo/bar”的东西,它可以在这两个平台和 MacOSX 上工作。

回答by Colin Hebert

You can use any path separator in Java, it will work on both Unix and Windows. If you still want to use the system path separator there is the File.separatorproperty which will give you the right one depending on the currentsystem.

您可以在 Java 中使用任何路径分隔符,它适用于 Unix 和 Windows。如果您仍然想使用系统路径分隔符,则File.separator可以根据当前系统为您提供正确的属性。

For the root, you can use listRoots()which gives you an array of root, there will be only one element on Unix systems, and as many as you have drives on Windows.

对于根,您可以使用listRoots()它为您提供一个根数组,在 Unix 系统上只有一个元素,而在 Windows 上有多少个驱动器。

回答by Jeroen Rosenberg

You can use the static field File.separator to retrieve the platform specific separator character for file paths

您可以使用静态字段 File.separator 来检索文件路径的平台特定分隔符

回答by Chris Dennett

Personally, I like to use the Path class from Eclipse for handling paths in general, which you can just use standalone with few modifications as it's quite isolated.

就我个人而言,我喜欢使用 Eclipse 中的 Path 类来处理一般的路径,因为它非常孤立,所以您可以单独使用并进行少量修改。

http://grepcode.com/file/repository.grepcode.com/java/eclipse.org/3.5/org.eclipse.equinox/common/3.5.0/org/eclipse/core/runtime/Path.java/?v=source

http://grepcode.com/file/repository.grepcode.com/java/eclipse.org/3.5/org.eclipse.equinox/common/3.5.0/org/eclipse/core/runtime/Path.java/?v =来源

回答by user207421

Just use /. I've been using it for 22 years. Never a problem.

只需使用/. 我已经使用它 22 年了。从来没有问题。

回答by ccDict

java 7 also supports the use of Pathshere

java 7 也支持使用Pathshere

The Path is obtained by invoking the getPath method of the default FileSystem.

Path 是通过调用默认 FileSystem 的 getPath 方法获得的。

You then may get a file from it by calling:

然后,您可以通过调用从中获取文件:

File fileSystemObtainedFile = Paths.get("C:\foo\bar.txt").toFile();

回答by NotAJavaGuy

Only 10 years too late.... The "It doesn't matter just use '/'" is only half true (ok, three quarters). You have to think about where your data is coming from.

只晚了 10 年......“没关系只使用'/'”只对了一半(好吧,四分之三)。您必须考虑数据来自何处。

If you get a path in Java, it will use '/' If you create a path in Java it will understand '/'

如果您在 Java 中获得路径,它将使用“/” 如果您在 Java 中创建路径,它将理解“/”

But what about paths that are input to your program?

但是输入到程序的路径呢?

Suppose I have a series of scripts that create a config (Properties) file. Suppose one of the config variables is INTERESTING_FILE, and we generate this to be a filename including path. Now suppose I want to extract the actual Filename from that, so I say

假设我有一系列创建配置(属性)文件的脚本。假设配置变量之一是 INTERESTING_FILE,我们将其生成为包含路径的文件名。现在假设我想从中提取实际的文件名,所以我说

String[] filename = INTERESTING_FILE.split("/");

This will misbehave on Windows Systems, however

但是,这在 Windows 系统上会出现异常

String[] filename = INTERESTING_FILE.split(pathSeparator);

will work (as will washing it through the Paths class).

会起作用(就像通过 Paths 类清洗它一样)。

I guess the point is, I wouldn't assume '/' is going to work in every case.

我想重点是,我不会假设 '/' 会在每种情况下都有效。