Java创建目录运行时WebApp
我有一个运行在JBOSS上的JavaSeam2网络应用程序 在我的应用程序中,我尝试动态/运行时创建目录 发生的情况是创建了这些目录,但当我尝试向其中写入文件时,会抛出一个空指针异常。 所以重启了我的服务器,一切正常,为什么Java创建目录运行时WebApp,java,directory,Java,Directory,我有一个运行在JBOSS上的JavaSeam2网络应用程序 在我的应用程序中,我尝试动态/运行时创建目录 发生的情况是创建了这些目录,但当我尝试向其中写入文件时,会抛出一个空指针异常。 所以重启了我的服务器,一切正常,为什么 if(ClassName.class.getClassLoader().getResource("roles/" + role ) == null) { //create directory with role name
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,所以它不会使用创建的新文件进行更新。从类加载器创建文件夹库和资源路径是一个坏主意(您永远不会知道srouce/文件夹来自何处)
更好的方法是,使用java.io.temp文件夹,或者使用配置来了解系统中的get repository文件夹。也许您应该发布相关的代码片段。