gpt4 book ai didi

java - 如何覆盖默认的 file.separator?

转载 作者:行者123 更新时间:2023-11-29 03:23:50 25 4
gpt4 key购买 nike

我使用的是 Windows 7。这是我的代码:

public class DriveLettersList {
public static void main( String[] args ) {
System.setProperty( "file.separator", "/" );

System.out.println( System.getProperty( "file.separator" ) );
System.out.println( Paths.get( "hello", "my", "word" ) );
}

输出令人困惑:

/
hello\my\word

为什么 Paths.get 返回 Windows 的默认路径分隔符?

最佳答案

注意 Paths.get(..) 的 javadoc

The details as to how the Strings are joined is provider specific but typically they will be joined using the name-separator as the separator.

在哪里可以检索名称分隔符

FileSystems.getDefault().getSeparator()

其中,在具有 WindowsFileSystem 的 Windows 上,它是 implemented as

@Override
public String getSeparator() {
return "\\";
}

对于这个 FileSystem 实现,您无法更改它。

这在 Unix 系统上可能有所不同。实际上,it seems it isn't

public final String getSeparator() {
return "/";
}

关于java - 如何覆盖默认的 file.separator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22118903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com