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
でしょうか?