簡體   English   中英

在Ubuntu中使用Java創建文件夾和文件

[英]Create a folder and a file with Java in Ubuntu

這是我想做的:

  1. 檢查文件夾是否存在
  2. 如果不存在,請創建文件夾
  3. 如果不存在,則不執行任何操作
  4. 最后在該文件夾中創建一個文件

在Windows 7中一切正常,但是當我在Ubuntu中運行該應用程序時,它不會創建文件夾,而只是使用文件夾名稱創建文件,例如:(我的文件名為xxx.xml,文件夾為d :\\ temp,因此在Ubuntu中,文件在d:生成,名稱為temp \\ xxx.xml。 這是我的代碼:

File folder = new File("D:\\temp");
if (folder.exists() && folder.isDirectory()) {
} else {
    folder.mkdir();
}

String filePath = folder + File.separator;
File file = new File(filePath + "xxx.xml");

StreamResult result = new StreamResult(file);
transformer.transform(source, result);
// more code here 

您的目錄(D:\\ temp)在Linux上不適用。

請考慮使用linux File System和File.SEPARATOR常量:

static String OS = System.getProperty("OS.name").toLowerCase();
String root = "/tmp";

if (OS.indexOf("win") >= 0) {
    root="D:\\temp";
} else {
    root="/";
}

File folder = new File(ROOT + "dir1" + File.SEPARATOR + "dir2");

if (folder.exists() && folder.isDirectory()) {
} else {
    folder.mkdir();
}

沒有嘗試過,但是誰可以工作。

D:\\temp在linux系統中不存在(我的意思是它將它解釋為好像是其他任何文件夾名稱一樣)

在Linux系統中,文件分隔符是/而不是\\ (對於Windows)

所以解決方案是:

File folder = new File("/tmp"); 

代替

File folder = new File("D:\\temp");

Linux不使用驅動器號(例如D :),而是使用正斜杠作為文件分隔符。

您可以執行以下操作:

File folder = new File("/path/name/of/the/folder");
folder.mkdirs(); // this will also create parent directories if necessary
File file = new File(folder, "filename");
StreamResult result = new StreamResult(file);

在類似Unix的系統上,沒有邏輯磁盤。 您可以嘗試在/tmp/home創建以下代碼,以在您的主目錄中創建temp目錄:

String myPathCandidate = System.getProperty("os.name").equals("Linux")? System.getProperty("user.home"):"D:\\";
  System.out.println(myPathCandidate);
  //Check write permissions
  File folder = new File(myPathCandidate);
  if (folder.exists() && folder.isDirectory() && folder.canWrite()) {
      System.out.println("Create directory here");
  } else {System.out.println("Wrong path");}

或者,對於/tmp tmp-系統臨時目錄。 大多數用戶可以在這里寫:

String myPathCandidate = System.getProperty("os.name").equals("Linux")? System.getProperty("java.io.tmpdir"):"D:\\";

在Java 7之前,File API可以利用操作系統配置(例如RAM磁盤上的臨時文件)創建臨時文件。 由於Java 7使用實用程序函數類Files

使用System類的getProperty靜態方法考慮兩種解決方案。

String os = System.getProperty("os.name");

if(os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0 || os.indexOf("aix") > 0 ) // Unix
    File folder = new File("/home/tmp"); 
else if(os.indexOf("win") >= 0) // Windows
    File folder = new File("D:\\temp");
else
    throw Exception("your message");

從Java 7開始,您可以將Files實用程序類與新的Path類一起使用。 請注意,以下示例中省略了異常處理。

// uses os separator for path/to/folder.
Path file = Paths.get("path","to","file");

// this creates directories in case they don't exist
Files.createDirectories(file.getParent());

if (!Files.exists(file)) {
    Files.createFile(file);
}

StreamResult result = new StreamResult(file.toFile());
transformer.transform(source, result);

這涵蓋了一般情況,如果不存在則創建一個文件夾,並在該文件夾上創建一個文件。


如果您確實要創建一個臨時文件(如示例中所示),則只需執行以下操作:

// this create a temporary file on the system's default temp folder.
Path tempFile = Files.createTempFile("xxx", "xml");

StreamResult result = new StreamResult(Files.newOutputStream(file, CREATE, APPEND, DELETE_ON_CLOSE));
transformer.transform(source, result);

請注意,使用這種方法,文件名將與您使用的前綴不完全對應(在本例中為xxx )。

盡管如此,由於它是一個臨時文件,所以這根本不重要。 DELETE_ON_CLOSE保證關閉文件時將其刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM