0

Possible Duplicate:
Platform independent paths in Java

I make a program but it save some files in a directory that the user selects.

I need to make this program to work both in linux and windows , so for example on windows the directory seperator is \ and on linux is / ...

How can I import one value that represents both depending the operating system?

Community
  • 1
  • 1
Vagelism
  • 601
  • 1
  • 16
  • 27
  • 2
    Have you tried using `/` on windows to see what happens? ;) – Peter Lawrey Mar 15 '12 at 09:38
  • Not currently I am using my code on linux. And I take an error. With the \ so probably with the / works! But I want to be sure for both situations. – Vagelism Mar 15 '12 at 10:02
  • `/` works on all platforms AFAIK. You only need the platform specific separator when you are exporting this path to a non-Java program. – Peter Lawrey Mar 15 '12 at 10:23

5 Answers5

2

File class offers you system dependent separators its as easy as

path = "images" + File.separatorChar + "cat.jpg"

Ruuhkis
  • 1,924
  • 1
  • 27
  • 46
1

If you want this, then you have to use

File.separator

This is a static field of File class of io package. For this purpose File has also some more static fields, like

pathSeparatorChar
pathSeparator
separatorChar

NOTE: The field names ended with Char are char typed and others are String typed just for the convinience.

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
1
char separatorChar = java.io.File.separatorChar

String separator = System.getProperty("file.separator");
Andrzej Jozwik
  • 14,331
  • 3
  • 59
  • 68
0

You can always use forward slashes. Windows supports them internally.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636