在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));
}
}
换个问题,如果/
可以在Unix和Windows上运行,为什么要使用File.separator
?