I am using the Apache Tomcat server. In a Java class in a library, I need to open a file to read it using getResourceAsStream(). Which directory should the file be in so it can be opened from the Java code, and what path should I use in the argument to getResourceAsStream()?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
This code reads test.properties file from the conf folder:
File file = null;
if (System.getProperty("catalina.base") != null)
file = new File(System.getProperty("catalina.base") + "/conf/test.properties");
else
throw new RuntimeException("Catalina.base doesn't exists.");
FileInputStream fis = new FileInputStream(file);
props.load(fis);
fis.close();