Java programming: Backslash vs. Forward slash and Windows vs. Linux

I usually work in Windows OS for my computer at home and use Linux for another computer at school. An annoying issue is that when I develop a Java application that reads a file or writes something into a file, I have to switch between forward slash and backslash if I take what I do at home to continue working at school. In Windows, backslash “\” is used for file path, but in Linux forward slash “/” is used.

To overcome this issue, Java programming language provides ”File.separator” that will automatically be set to a forward slash in Linux and a backslash in Windows.

This is just a little trick that many people may overlook.  

This entry was posted in Java. Bookmark the permalink.

6 Responses to Java programming: Backslash vs. Forward slash and Windows vs. Linux

  1. Allen says:

    Thank you. Worked for me.

  2. dude says:

    What about properties file?

  3. madmayr says:

    thanks, good hint!

  4. Pingback: Back to Java (for Android) | soledad penadés

  5. Sikinder says:

    Thank you. this is very useful hint.

  6. Lildirt says:

    Cheers! :)
    - Lildirt

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s