Java - creating a folder and file in web server directory

12,152
String realPath = getServletContext().getRealPath("/");
File file = new FIle(realPath+"/TestFolder", "testFIle.txt");
file.mkdirs();

The above code will create a folder TestFolder and a file testFIle.txt in the root directory of your web application.

file.mkdirs() Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

Returns: true if and only if the directory was created, along with all necessary parent directories; false otherwise

Hope this helps !

Share:
12,152
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    i would like to create a file in a folder (TestFolder) of web server. I will i get relative path of web server directory and how will i create my TestFolder in that directory.

    currently i will create a folder in my local server but later i need to create a folder in test environment (server could be different on test environment)