2014-09-23 62 views
0

我一直在嘗試在某些目錄內創建文件,但一直未能弄清楚。文件管理 - 管理java的文本文件

重點是在包dircom.resources.files內創建文本文件,但我的想法不起作用。

public boolean archiveFile() { 
      // 
     InputStream str = this.getClass().getResourceAsStream("/com/resources/files/"+Filename.txt 
     boolean bol = false; 

      file = new File( str.toString() ); 
     if(!file.exists()) { 
      try { 
        file.createNewFile(); 
        bol = true; 
      } catch (Exception e) { 
       e.printStackTrace(); 
      }// try-catch   
     }else { 
      bol = false; 
     } // if - else 
      return bol;   
    }// archiveFile 
+1

new File(str.toString());將無法工作。你有一個流,那麼你必須從那裏讀取。請參閱http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string – Leo 2014-09-23 23:19:49

+0

getClass()。getResourceAsStream是做什麼的?第一次看到這 – Coffee 2014-09-23 23:24:46

回答

1

資源不是文件,資源路徑也不是目錄。考慮WAR文件正在執行而不解壓縮的情況。

你不能這樣做。

+0

好,那麼你可以給我一個鏈接,幫助我瞭解什麼是正確的程序是好嗎? – shep 2014-09-24 05:31:45