1.InputStream is=new FileInputStream("lib/config.properties");可以指定绝对路径
2. InputStream is = this.getClass().getResourceAsStream("/config.properties");可以直接放在src下面读取到的路径就是web-inf下面的classes相对路径
E 资源配置文件在classes下
InputStream in = this.class.getClassLoader().getResourceAsStream("config.properties");
注意事项:如上以/开头的是指从根目录开始加载。
D 使用类加载器的方式
InputStream in = Main.class.getClassLoader().
getResourceAsStream("test/resource/config.properties");
指定加载资源配置文件的classes相对路径
InputStream in =
Main.class.getResourceAsStream("/test/resource/config.properties");
java代码:
Properties prop = new Properties();
InputStream is = null;
is=new FileInputStream("lib/config.properties");
prop.load(is);
Class.forName(prop.getProperty("driverifx"));
Class.forName(prop.getProperty("driveroracle"));
this.oracleConn = DriverManager.getConnection(prop.getProperty("urloracle"), prop.getProperty("usernameoracle"), prop.getProperty("userpwdoracle"));
if (this.oracleConn != null) {
logger.info("CONNECT ORACLE SUCCESS");
} |