简体   繁体   English

在Linux下,不会通过Java代码创建文件夹和文件,但适用于Windows

[英]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 我对Java的io.file类有一些疑问

  1. Linux: does the file seperator operator '//' work or do we need to use '\\'? Linux:文件分隔符'//'是否起作用,还是需要使用'\\'?
  2. In red hat there is no drive partion name so how to give the Linux equivalent of the Windows path 'c://TempFolder//Images//'? 在红帽中没有驱动器分区名称,因此如何给Linux等效于Windows路径“ c:// TempFolder // Images //”?

You can use the static field File.separator or, better, use the nio Paths class like this: 您可以使用静态字段File.separator或更好地使用nio Paths类,如下所示:

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: 为了让某些东西可以引用绝对路径,可以使用File.separator来启动String参数,使用nio这个方法也可以得到它:

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

Under Windows: 在Windows下:

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

Under Linux: 在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. 之所以具有两个反斜杠( \\\\ ),是因为在字符串中必须转义一个反斜杠: \\t是制表符等。

There are no drive letters in Linux, if that was your question. 如果这是您的问题,则Linux中没有驱动器号。 For temporary files you might use File.createTemporaryFile or createTemporaryDirectory. 对于临时文件,您可以使用File.createTemporaryFile或createTemporaryDirectory。

Directories on other computers may also be used without drive letters, but with UNC paths: 其他计算机上的目录也可以不带驱动器号,但带UNC路径使用:

Windows : Windows

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

Linux : Linux

//Server/Directory/Directory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java EWS API可在Windows下运行,但不能在Linux上运行 - Java EWS API works under windows but not on linux 在Linux中从带有锁定符号的Java代码文件创建 - From java code file getting created with a lock symbol in Linux 我的java代码在linux中正常工作但在Windows上不能正常工作 - My java code works properly in linux but not on Windows getBytes() 在 Windows (Java9) 下按预期工作,在 Linux 下也不工作 - getBytes() works as expected under Windows (Java9) and does nor works under Linux java代码搜索整个系统的文件,在windows上工作正常但在linux ubuntu中无限 - java code to search a file entire system, works fine on windows but infinite in linux ubuntu 在Linux上运行的Java代码中,如何指向共享的Windows文件夹? - In Java code running on Linux, how to point to a shared Windows folder? Java URLConnection适用于Windows,但不适用于Linux - Java URLConnection works with windows,but not with linux Java无法从Linux在Windows共享文件夹中创建文件 - Java Not able to create a file in windows shared folder from Linux 在Windows下如何在Java中运行Linux程序? - How to run linux program in java under windows? 在Windows上运行但不在Linux上运行的Java代码 - Java code running on windows but not on linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM