使用文件有什么区别。分隔符和正常的/在Java路径字符串?
与双反斜杠相比,平台独立性似乎不是原因,因为这两个版本都可以在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?