简体   繁体   中英

How to give relative path for my file in java file?

I am using tomcat server for my java application(Servlet,jsp).In my servlet page calling one java class function.Actually the java file is written separately and i will call the function written inside it.With in the function, i have to read one config(user defined) file for some purpose.so, i am using File class for that.

But, here i have to give relative path of the config file(user defined).Because, now i am running this application in local windows server.But my live server is based on Linux.So, the file path is changed in linux.

File f1=new File("D:\tomcat\webapp\myapp\WEB-INF\src\point_config.txt");  -- windows
File f1=new File("D:\ravi\tomcat\webapp\myapp\WEB-INF\src\point_config.txt");  -- linux

So, i have to give relative path of the file that is common to both windows and linux machine.

Is there a way to do this?

Please guide me to get out of this issue?

Place your config file under your webapp WEB-INF/classes folder and read like this in code

InputStream is= 
   YourClassName.class.getResourceAsStream("point_config.txt");

The path of the config file leads into the WEB-INF folder

tomcat\webapp\myapp\WEB-INF\src\point_config.txt

Anything inside WEB-INF is protected and cannot be user-defined once the web application has launched. If you meant to read from a user-defined configuration file from the file system, please use an API like the common configuration API .

If you want to insist on keeping the file inside the WEB-INF folder, use the Class.getResourceAsStream() method to obtain the configuration instead. That would not make the configuration user-defined though.

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