使用
Properties p = new Properties();
try {
String configPath = this.getClass().getClassLoader().getResource("config.properties").getFile();
InputStream in = new FileInputStream(configPath);
p.load(in);
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(p.getProperty("mail.charset"));
异常:
java.io.FileNotFoundException: E:\myeclipse%2010.7\workspace\test\WebRoot\WEB-INF\classes\config.properties (系统找不到指定的路径。)
%20是空格,所以有问题需要进行编码转换。
出现这个的原因是路径中有空格,想我的路径就是myeclipse 10.7\.....
解决这个问题就要对路径进行转码,则如下:
Properties p = new Properties();
try {
String configPath = this.getClass().getClassLoader().getResource("config.properties").getFile();
configPath = java.net.URLDecoder.decode(configPath,"utf-8");
InputStream in = new FileInputStream(configPath);
p.load(in);
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(p.getProperty("mail.charset"));