How to create a file inside the web-app

Posted on 2003-11-27
Last Modified: 2010-03-31
I have a java class that resides inside com.pappu.kumar package , inside my web-app (name=test). So the structure is something like :


Now in CreateFile.class , i try to create a file . What path should i give so that i can create the file in the "files" directory , i would like to use a way that works both on unix and windows. And if its not possible to create under "files" , whats the easiest way to create the file in some folder ??
Question by:rahulkothari
  • 5
LVL 92

Expert Comment

ID: 9834070
You need access to the ServletContext for your webapp, and use it's getRealPath() method.
LVL 92

Expert Comment

ID: 9834083
File filesdir = context.RealPath("/files");
LVL 92

Expert Comment

ID: 9834085
sorry that shopuld have been:

File filesdir = context.getRealPath("/files");
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 86

Expert Comment

ID: 9834113
>>File filesdir = context.getRealPath("/files");

are you sure it's not

File filesdir = context.getRealPath("../files");

LVL 92

Accepted Solution

objects earned 125 total points
ID: 9834131
Actually it is:

File filesdir = new File(context.getRealPath("/files"));
LVL 92

Expert Comment

ID: 9890016

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 172
throw exception 21 58
mysql jsp example issue 32 48
Is there a simpler dropbox system? 10 34
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question