properties 文件读取
假设在SRC根目录下有文件 config.properties ,下面代码就是读取这个文件的例子程序
方法1
Properties prop = new Properties(); InputStream in = Object.class.getResourceAsStream("/config.properties"); prop.load(in); String databasePath = prop.getProperty("DATA_SOURCE").trim(); //Object 就是指定的类,下面的方法会去指定类的包下找叫config.properties的配置文件InputStream in = Object.class.getResourceAsStream("config.properties"); String fp = this.getClass().getClassLoader().getResource("/").getPath() + "config.properties";File file = new File(fp);Properties properties = new Properties();properties.load(new FileInputStream(file));String urlfilter = properties.getProperty("DATA_SOURCE");//MyObject是项目里面的类String fp = MyObject.getClass().getClassLoader().getResource("/cn/myproject/").getPath() + "config.properties";File file = new File(fp);Properties properties = new Properties();properties.load(new FileInputStream(file));String urlfilter = properties.getProperty("DATA_SOURCE");