我项目中,有Properties  txe文件,
以前学过用Config 和Properties都可读取,这Config 和Properties这二个有什么区别,分别怎么用
thanks

解决方案 »

  1.   


     public static void main(String[] args)
        { //相对路径,一般跟java源文件放到一起,然后一起编译到class path目录下
    Properties p = new Properties();
    try
    {
    String fileName="config.proterties";
    InputStream is = PropertiesTest.class.getResourceAsStream(fileName);
    //我这里是静态方法,不能用getClass.getResourceAsStream(fileName),注意相对路径
    p.load(is);
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    String key="key";
    //取值 
    String value=p.getProperty(key);
        }
      

  2.   

    +++
    另外补充一点PropertiesTest.class.getResourceAsStream(fileName);只读取一次文件,也就是说当你的fileName文件内容有更改时他是不会再去检测的,
    你可以用PropertiesTest.class.getResource(fileName).getPath()来得到文件的真实路径然后通过FileInputStream去读取,这样就可以解决上面提到的问题;