相关疑难解决方法(0)

File.separator与路径中的斜杠之间的区别

在Java Path-String中使用File.separator和普通/有什么区别?

与双反斜杠\\平台相比,独立性似乎不是原因,因为两个版本都可以在Windows和Unix下运行.

public class SlashTest {
    @Test
    public void slash() throws Exception {
        File file = new File("src/trials/SlashTest.java");
        assertThat(file.exists(), is(true));
    }

    @Test
    public void separator() throws Exception {
        File file = new File("src" + File.separator + "trials" + File.separator + "SlashTest.java");
        assertThat(file.exists(), is(true));
    }
}
Run Code Online (Sandbox Code Playgroud)

要重新解释这个问题,如果/适用于Unix和Windows,为什么要使用File.separator

java

189
推荐指数
7
解决办法
28万
查看次数

标签 统计

java ×1