简体   繁体   中英

Current directory of a web app in netbeans

I am working on a web app i have java files in it which uses certain files.I want to specify these files using relative path in java so that it doesn't produce mobility issue.But Where should i place a file in a web app so that i can use relative path.? I have tried placing the files under source package , web folder , directly under the web-application.Please help.Thanks in advance

The simplest way to get the current directory of a java application is :

System.out.println(new File(".").getAbsolutePath());

Like that you can consider the given path as the root of your application.

Cheers, Maxime.

Read the file as a resource. Put it somewhere in the src. For instance

src/resources/myresource.txt

Then you can just do

InputStream is = getClass().getResourceAsStream("/resources/myresource.txt");

Note: if you are using maven, then you are more accustomed to something like this

src/main/resources/myresource.txt

With maven, everything in the main/resources folder gets built to the root, so you would leave out the resources in your path

InputStream is = getClass().getResourceAsStream("/myresource.txt");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM