在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?
如何从去jar:file:/C:/Program%20Files/test.jar!/foo/bar到一个File指向C:/Program Files/test.jar?