使用反射获取类加载器来读取properties文件出现空指针异常,可以使用直接使用输入流读取properties文件,为什么教学视频中却可以使用反射?另外问下怎么在PC端提问,不是论坛发帖,单纯悬赏C币提问,我都是在手机上提,电脑上修改
@Test
public void getConnection() throws Exception {
/*
* 读取配置文件来获取数据库连接
*/
Properties properties = new Properties();
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
InputStream in = this.getClass().getClassLoader().getResourceAsStream("C:/Java/WprkSpace/JDBC/jdbc.properties");
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
Driver driver = (Driver) Class.forName(driverClass).newInstance();
properties.put("user", user);
properties.put("password", password);
Connection connerction = driver.connect(jdbcUrl, properties);
System.out.println( connerction);
in.close();
}
读取properties文件可以有多种方式,用IO或者用ClassLoader,绝对路径的用IO,相对路径可以用IO也可以用ClassLoader,因为相对路径是相对于Classpath的。一般来说用到ClassLoader的话都是用的相对路径了。
http://bbs.csdn.net/topics/391984276
路径改成相对于SRC的路径吧。
classloader去加载的是classpath下的资源,加*会加载jar中的,否则会扫描普通文件,希望对你理解有帮助
一般没有绝对路径这种用法吧,非要用试一试
Properties props = new Properties();
File file=new File("H:\\DESKTOP\\email.properties");
props.load(this.getClass().getClassLoader().getResourceAsStream(file.getName()));
1,把jdbc.properties 放到src下 ,
2.
public class CMConstant {
public static String getConfigureParameterFromJDBC(String paramString) {
String str = CMConstant.getRootPath() + File.separator + "WEB-INF"
+ File.separator + "classes/config.properties";
Properties localProperties = new Properties();
try {
FileInputStream localFileInputStream = new FileInputStream(str);
localProperties.load(localFileInputStream);
localFileInputStream.close();
} catch (FileNotFoundException localFileNotFoundException) {
logger.error("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
localFileNotFoundException.printStackTrace();
return null;
} catch (IOException localIOException) {
logger.error("装载文件--->失败!");
localIOException.printStackTrace();
}
return localProperties.getProperty(paramString);
}
public static String getRootPath() {
String result = null;
try {
result = CMConstant.class.getResource("CMConstant.class").toURI()
.getPath().toString();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
int index = result.indexOf("WEB-INF");
if (index == -1) {
index = result.indexOf("bin");
}
result = result.substring(1, index);
if (result.endsWith("/"))
result = result.substring(0, result.length() - 1);// 不包含最后的"/"
return result;
}
/**
* 查找config/jdbc.properties里值
* @Description: @param key
* @Description: @return
* @Last Modified: , Date Modified:
*/
public static String getJDBCValue(String key) {
String filePath = getRootPath() + File.separator + "WEB-INF\\classes"
+ File.separator + "config.properties";
Properties propertie = new Properties();
try {
FileInputStream inputFile = new FileInputStream(filePath);
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
return null;
} catch (IOException ex) {
ex.printStackTrace();
}
return propertie.getProperty(key);
}
}