Solved

How to create a file inside the web-app

Posted on 2003-11-27
6
223 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to start Neon 20 89
printing a file in reverse order is easy in recursion rather than in iteration 3 52
egit plugin on eclipse 8 41
fibonacci ten numbers 4 30
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now