Solved

How to create a file inside the web-app

Posted on 2003-11-27
6
224 Views
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 :

test
  |
  |--jsp
  |
  |--files
  |
  |--WEB-INF
        |
        |--lib
        |--classes
              |--com
                   |--pappu
                        |--kummar
                              |-CreateFile.class  

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 ??
0
Comment
Question by:rahulkothari
  • 5
6 Comments
 
LVL 92

Expert Comment

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

Expert Comment

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

Expert Comment

by:objects
ID: 9834085
sorry that shopuld have been:

File filesdir = context.getRealPath("/files");
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 86

Expert Comment

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

are you sure it's not

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

?
0
 
LVL 92

Accepted Solution

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

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

Expert Comment

by:objects
ID: 9890016
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 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