开发者

Java Create Directory Runtime WebApp

I have a webapp in Java SEAM2 running on JBOSS.

In my application I am trying to create directories on the fly / runtime.

Well what happens is that these directories get created, but when I try to write a file into them it throws a null pointer exception. So restarted my server and all works well then, why is it ?

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.SEVE开发者_如何学PythonRITY_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;
            }


        }

My guess is that since I am using getClassLoader it is not getting updated with the new files created


It is a bad idea to create a folder base a resource path from class loader (you never where is the srouce/folder from) better way are, use java.io.temp folder, or use a configuration to know get repository folder in your system.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜