简体   繁体   中英

Folder and file are not getting created by Java code under Linux, but it works for Windows

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//'?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM