test.properties文件在类的同一个包下,因为完整路径是:
E:\Documents and Settings\Administrator\My Documents\Genuitec\MyEclipse 6.6\Java_Collection\bin\com\note\test.properties
由于部分文件夹名太长,所以得到的path值为:
/E:/Documents%20and%20Settings/Administrator/My%20Documents/Genuitec/MyEclipse%206.6/Java_Collection/bin/com/note/DBConn.properties
这样FileInputStream(path)就会找不到路径.public class PropertiesTest { public static void main(String[] args) {
Properties pps = new Properties();
try {
//加载文件
String path = PropertiesTest.class.getResource("test.properties").getFile();
System.out.println(path);
pps.load(new FileInputStream(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}}
当然如果说要读取键值可以用
InputStream iStream = PropertiesTest.class.getResourceAsStream("test.properties");
pps.load(iStream);
但是现在想要完整的路径应该如何实现呢?
怎样得到的路径是:
/E:/\Documents and Settings/Administrator/My Documents/Genuitec/MyEclipse 6.6/Java_Collection/bin/com/note/test.properties
而不是
/E:/Documents%20and%20Settings/Administrator/My%20Documents/Genuitec/MyEclipse%206.6/Java_Collection/bin/com/note/DBConn.properties
E:\Documents and Settings\Administrator\My Documents\Genuitec\MyEclipse 6.6\Java_Collection\bin\com\note\test.properties
由于部分文件夹名太长,所以得到的path值为:
/E:/Documents%20and%20Settings/Administrator/My%20Documents/Genuitec/MyEclipse%206.6/Java_Collection/bin/com/note/DBConn.properties
这样FileInputStream(path)就会找不到路径.public class PropertiesTest { public static void main(String[] args) {
Properties pps = new Properties();
try {
//加载文件
String path = PropertiesTest.class.getResource("test.properties").getFile();
System.out.println(path);
pps.load(new FileInputStream(path));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}}
当然如果说要读取键值可以用
InputStream iStream = PropertiesTest.class.getResourceAsStream("test.properties");
pps.load(iStream);
但是现在想要完整的路径应该如何实现呢?
怎样得到的路径是:
/E:/\Documents and Settings/Administrator/My Documents/Genuitec/MyEclipse 6.6/Java_Collection/bin/com/note/test.properties
而不是
/E:/Documents%20and%20Settings/Administrator/My%20Documents/Genuitec/MyEclipse%206.6/Java_Collection/bin/com/note/DBConn.properties
InputStream is = ClassLoader.getSystemResourceAsStream("com/note/DBConn.properties");
多谢提醒啊,但是怎样回避这个本地URL协议呢?我就是想要完整路径.
PS:我就知道会有人被DBConn.properties 这个文件名误导,这个只是测试用的,发帖子时就改成test.properties,还是少了最后一个没改..哈哈
File file = new File(path);
file.getFile()).getAbsoluteFile() --> 这个就是你要的本地绝度路径
file.getAbsoluteFile();
String path = PropertiesTest.class.getResource("DBConn.properties").getFile();
File file = new File(path);
System.out.println(file.getAbsolutePath());
这样得到的还是:
E:\Documents%20and%20Settings\Administrator\My%20Documents\Genuitec\MyEclipse%206.6\Java_Collection\bin\com\note\DBConn.properties
不是完整路径
URLDecoder.decode(file.getAbsolutePath(), "GBK")
URL是不能包含空格的,通过URLDecoder把那些%20变成空格,转换成原来的形式而已。所以你这样写也直接解码 PropertiesTest.
class.getResource("test.properties").getFile()System.out.println(URLDecoder.decode(PropertiesTest.class.getResource("test.properties").getFile(), "ISO8859-1"));既然你说你不用中文,所以编码方式改成"ISO8859-1",这样结果是一样的。
当然,如果你把编码改了,如果路径里面有中文就解码不出来。