Difference between File.separator and slash in paths

What is the difference between using File.separator and a normal / in a Java Path-String?

In contrast to double backslash \\ platform independence seems not to be the reason, since both versions work under Windows and 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));
    }
}

To rephrase the question, if / works on Unix and Windows, why should one ever want to use File.separator?

14 Answers
14

Leave a Comment