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

解决方案 »

  1.   

    getResource 返回的是个 URL 对象,又不是 File 对象啊,所以 URL#getFile 返回的是个 URL 的路径
      

  2.   

    由于是本地文件,所以采用文件的 URL 协议,所以这个资源完整的 URL 是:file:/E:/Documents%20and%20Settings/Administrator/My%20Documents/Genuitec/MyEclipse%206.6/Java_Collection/bin/com/note/DBConn.properties由于 getFile 只返回路径,前面的协议名称就去掉了。建议采用 getResourceAsStream 方法。而且你这样做存在一定的风险,如果不打成 jar 包,运行是没有问题的,如果打成 jar 包运行就会出错。建议改成:InputStream is = ClassLoader.getSystemResourceAsStream("com/note/DBConn.properties");PS:配置文件不应该放到包中去的,应该放在 src 的外面,这样发布后也能修改配置文件。
      

  3.   


    InputStream is = ClassLoader.getSystemResourceAsStream("com/note/DBConn.properties");
    多谢提醒啊,但是怎样回避这个本地URL协议呢?我就是想要完整路径.
    PS:我就知道会有人被DBConn.properties 这个文件名误导,这个只是测试用的,发帖子时就改成test.properties,还是少了最后一个没改..哈哈
      

  4.   

    沉了我就UP UP UP ..
      

  5.   

    用取出来的path新建一个File就可以了。
    File file = new File(path); 
    file.getFile()).getAbsoluteFile() --> 这个就是你要的本地绝度路径
      

  6.   

    有写笔误~~ File file = new File(path);
    file.getAbsoluteFile();
      

  7.   


    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
    不是完整路径
      

  8.   

    再加上下面这行吧,把URL decode。 
    URLDecoder.decode(file.getAbsolutePath(), "GBK")
      

  9.   

    没必要转什么码,从来也不用GBK
      

  10.   

    这里不是没必要,而是你的问题本来就是和URL Decoder有关,你一开始就已经是绝对路径,只不过是URL形式罢了。
    URL是不能包含空格的,通过URLDecoder把那些%20变成空格,转换成原来的形式而已。所以你这样写也直接解码 PropertiesTest.
    class.getResource("test.properties").getFile()System.out.println(URLDecoder.decode(PropertiesTest.class.getResource("test.properties").getFile(), "ISO8859-1"));既然你说你不用中文,所以编码方式改成"ISO8859-1",这样结果是一样的。
    当然,如果你把编码改了,如果路径里面有中文就解码不出来。