如何在java Web应用程序项目结构中从代码创建文件夹?

时间:2013-10-24 07:29:37

标签: java

任何人都可以建议如何从代码中创建java web应用程序项目结构中的文件夹?

3 个答案:

答案 0 :(得分:2)

您正在讨论在servletcontainer / webappContext中创建文件夹。 这是怎么做的:

public void doPost(HttpServletRequest req,HttpServletResponse resp){
      String path = req.getServletContext().getRealPath("/");
      File f = new File (path +"myNewFolder");
      f.mkdir();

}

答案 1 :(得分:1)

发现这个:

File f = new File("C:\\Test");
try{
  if(f.mkdir()) { 
    System.out.println("Directory Created");
  } else {
      System.out.println("Directory is not created");
  }
 } catch(Exception e){
  e.printStackTrace();
}

How to create a folder in Java?

希望有所帮助

答案 2 :(得分:0)

您可以这样做:

    String absolute = getClass().getProtectionDomain().getCodeSource().getLocation().toExternalForm();
    absolute = absolute.substring(0, absolute.length()-1);
    absolute = absolute.substring(0, absolute.lastIndexOf("/")+1);
    String xmlPath = absolute + "webapp/xml/";       
    String os = System.getProperty("os.name");
    if(os.indexOf("Windows") != -1) {
        xmlPath = xmlPath.replace("/", "\\\\");
        if(xmlPath.indexOf("file:\\\\") != -1) {
        xmlPath = xmlPath.replace("file:\\\\", "");
        }
    } else if(xmlPath.indexOf("file:") != -1) {
        xmlPath = xmlPath.replace("file:", "");
    }

    File f = new File(xmlPath);
    f.mkdir();