This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes create a file in the root directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "create a file in the root directory" Watch "create a file in the root directory" New topic

create a file in the root directory

Praveen Sharma
Ranch Hand

Joined: Jul 31, 2008
Posts: 129
based on some user action I need to create a file on the server inside the project's root directory. The url of my application is :

The name of the file is DeskItems.xml.

I tried to get the application Context path and appended that to the file name by using :
getServletContext.getPath("/") + "/DeskItems.xml";

But this approach is not working and I get an exception saying FileNotFound. I am using Tomcat server.

Please help me out in resolving this issue.

SCJP 80% SCWCD 89%
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12682
Look at the ServletContext getRealPath() method JavaDocs.


Java Resources at
I agree. Here's the link:
subject: create a file in the root directory
Similar Threads
Creating a JAR file contains TLD-file.
Saving file at server
Simple Servlet Not Working
Save a file on the application root
Directory Creation on FTP Server using Java Program