简体   繁体   中英

To get the root path of file in java

I am using eclipse Indigo IDE ..I am developing a jsp application ( myProject ) using eclipse in which i have a property file ( myProject\\webcontent\\db.properties ) for configuring database credentials.

I am trying to access this file from a class ( myProject\\src\\samplePackage\\sampleDBConnect.java ). I have exported samplePackage.jar into * myProject\\webContent\\WEB-INF\\lib* .

I have a Test.jsp page which calls a method in sampleDBConnect.java.

When i am trying to run this Test.jsp page, the current directory path shows C:\\Documents and Settings\\username . I have my project in some other drive(E:).

Can someone tell me how to access the properties file....

getClass().getResourceAsStream("db.properties");

This assumes your db.properties is in the same directory "samplePackage".

If you want to keep it in src directory or say resource directory then use

getClass().getClassLoader().getResourceAsStream("db.properties");

Since your java code belongs to the same web project you don't have to create a jar of the same project and place that in the WEB-INF/lib folder

将db.properties移动到myProject \\ src目录,java代码:InputStream input = sampleDBConnect.class.getClassLoader()。getResourceAsStream(“db.properties”);

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