0

I have a few questions about Java's io.file class

  1. Linux: does the file seperator operator '//' work or do we need to use '\'?
  2. In red hat there is no drive partion name so how to give the Linux equivalent of the Windows path 'c://TempFolder//Images//'?
2
  • Linux file system has a single root - / Even NTFS volumes are mounted are under that root. The file separator on Linux is '/'. Some Java software will work with '/' on both OS systems. However, in Windows, the proper representation is '\\' Commented Mar 19, 2014 at 19:04
  • File separator is the single forward slash / Commented Mar 19, 2014 at 19:04

2 Answers 2

3

You can use the static field File.separator or, better, use the nio Paths class like this:

File f = Paths.get( "dir1", "dir2", "dir3" ).toFile();

To get something to refer to the absolute path, start the String arguments with a File.separator, which you might get also with nio with this method:

http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystem.html#getSeparator%28%29

Sign up to request clarification or add additional context in comments.

Comments

2

Under Windows:

File file = new File("C:\\TempFolder\\Images");
File file = new File("C:/TempFolder/Images"); // Because Windows soemtimes is nice.

Under Linux:

File file = new File("/TempFolder/Images");

The reason having two backslashes (\\), is that in strings a backslash must be escaped: \t being a tab character etcetera.

There are no drive letters in Linux, if that was your question. For temporary files you might use File.createTemporaryFile or createTemporaryDirectory.

Directories on other computers may also be used without drive letters, but with UNC paths:

Windows:

\\Server\Directory\Directory
"\\\\Server\\Directory\\Directory"

Linux:

//Server/Directory/Directory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.