路径中 File.separator 和斜杠之间的区别
2022-08-31 05:56:34
在 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