2010-10-18 47 views
0

我在運行於JBOSS上的Java SEAM2中有一個web應用程序。Java創建目錄運行時Web應用程序

在我的應用程序中,我試圖在運行時創建目錄。

那麼會發生什麼是這些目錄被創建,但是當我嘗試寫入一個文件時,它會拋出一個空指針異常。 因此,重新啓動我的服務器,一切正常,那麼爲什麼?

if(ClassName.class.getClassLoader().getResource("roles/" + role) == null) 
     { 
      //create directory with role name 
      String rolePath = ClassName.class.getClassLoader().getResource("roles").getPath(); 
      rolePath = rolePath.substring(1, rolePath.length()); 
      rolePath = rolePath.replace("/", "\\"); 
      rolePath = rolePath + "\\" + role; 

      if(!(new File(rolePath).mkdir())) 
      { 
       this.addMessage(FacesMessage.SEVERITY_ERROR, "Error Creating Role Directory"); 
       return; 
      } 

     } 



     if(ClassName.class.getClassLoader().getResource("roles/" + role + "/" + app) == null) 
     { 
      String appPath = ClassName.class.getClassLoader().getResource("roles/" + role).getPath(); 
      appPath = appPath.substring(1, appPath.length()); 
      appPath = appPath.replace("/", "\\"); 
      appPath = appPath + "\\" + app; 

      File appFolder = new File(appPath); 
      if(!(appFolder.mkdir())) 
      { 
       this.addMessage(FacesMessage.SEVERITY_ERROR, "Error Creating Role Directory"); 
       return; 
      } 


     } 

我的猜測是,由於我使用getClassLoader它是沒有得到新的文件更新創建

+0

也許你應該發佈相關的代碼片段。 – Raghuram 2010-10-18 05:53:08

回答

1

這是一個壞主意,從來沒有在那裏建立一個文件夾基地的類加載器的資源路徑(你是更好的方法是使用java.io.temp文件夾,或者使用配置來知道系統中的獲取存儲庫文件夾。