2

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)

1
  • 3
    Please post code showing what you tried, with the errors you got if any, and describing the expected/actual behavior of the code Commented May 10, 2012 at 11:44

1 Answer 1

8
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 !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.