2016-08-03 51 views
0

我試着用這些方法創建文件:這段代碼爲什麼不創建文件?

private boolean createFileMain(String path){ 
    File file = new File(path); 
    if(file.isDirectory()){ 
     return this.createDirectory(file); 
    } else if(file.isFile()) { 
     return this.createFile(file); 
    } else { 
     return false; 
    } 
} 

private boolean createFile(File file){ 
    if(!file.exists()){ 
     if(file.getParentFile().exists()){ 
      try{ 
       if(file.createNewFile()){ 
        return true; 
       } 
      }catch(IOException e){ 
       return false; 
      } 
     } else { 
      if(this.createDirectory(file)){ 
       this.createFile(file); 
      } else { 
       return false; 
      } 
     } 
    } 
    return true; 
} 

private boolean createDirectory(File file){ 
    if(!file.exists()){ 
     if(file.mkdirs()){ 
      return true; 
     } 
     return false; 
    } 
    return true; 
} 

的文件的路徑:

/用戶/用戶名/目錄/帳號/

/用戶/用戶名/目錄/ SRCS/FILE1.TXT

/Users/username/Directory/file2.txt

當我嘗試運行這個時,下面的方法拋出一個StackOverFlowError

public void writeInFile(String path, List<String> content) { 
    if ((new File(path)).exists()) { 
     try { 
      writer = new PrintWriter(path, "ASCII"); 
      for (String contentItem : content) { 
       writer.println(contentItem); 
      } 
      writer.close(); 
     } catch (FileNotFoundException e1) { 
      //DO STUFF 
     } catch (UnsupportedEncodingException e) { 
      //DO STUFF 
     } 
    } else { 
     this.createFileMain(path); 
     this.writeInFile(path, content); 
    } 

爲什麼沒有創建文件?

+2

你有調試嗎?另外:在你的catch塊中添加日誌記錄,至少你會被告知是否出現問題 – Stultuske

回答

0

您的createFileMain只創建已存在的文件。

您不需要創建要寫入的文件,只需要將其寫入目錄即可。

public void writeInFile(String path, List<String> content) { 
    File file = new File(path); 
    File parent = file.getParentFile(); 
    if (parent != null && !parent.exists()) 
     parent.mkdirs(); 

    try (PrintWriter writer = new PrintWriter(path, "ASCII")) { 
     for (String contentItem : content) { 
      writer.println(contentItem); 
     } 
     writer.close(); 
    } catch (IOException e1) { 
     //DO STUFF 
    } 
} 
2

您是否閱讀過isDirectory()等的JavaDocs?對於isDirectory()它說:

返回true當且僅當該抽象路徑名錶示的文件存在並且是一個目錄;否則爲假

因此,如果該目錄不存在,您將獲得false並且不會創建一個。然後你繼續嘗試寫和創建和寫等,直到你得到StackOverFlowError。

要修復計算器,應該檢查創建文件的返回值,例如

boolean created = this.createFileMain(path); 
if(created) { 
    this.writeInFile(path, content); 
} 

要解決你的文件/目錄的創建你需要檢查文件是否已經存在,否則創建(也可通過file.getParentFile().mkdirs()創建父目錄)。

問題是,你應該知道你是否想創建一個文件或一個目錄,因爲你不能單獨通過名稱來判斷該路徑是否是一個目錄或文件名(除非你創建了一些標記如總是以分隔符結尾目錄路徑或要求文件始終有擴展名)。如果你想寫一些你需要創建一個文件的內容,一個目錄會再次破壞你的代碼。

0
private boolean createFile(File file) { 
    if(!file.exists()) { 
     if(file.getParentFile().exists()) { 
      // ... 
     } else { // In this case, neither file nor its parent exist 
      if(this.createDirectory(file)) { 
       this.createFile(file); // HERE, you're calling the same method again 
      } else { 
       // ...; 
      } 
     } 
    } 
    return true; 
} 

我相信你要替換的行標HEREthis.createFile(file.getParentFile());。這裏發生的事情是你的函數用相同的參數遞歸地調用它自己,所以什麼都沒有發生,並且你的程序在一個循環中被卡住,直到它用完堆棧內存。