Properties类

来源:互联网 发布:linux查询逻辑cpu个数 编辑:程序博客网 时间:2024/05/28 06:07

mark一下

this.getClass().getClassLoader().getResourceAsStream(“properties.properties”);
加载的是src目录下的文件。

package Properties;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;import java.util.Properties;public class PropertiesTest {    public static void main(String[] args) throws IOException {        PropertiesTest t=new PropertiesTest();        t.testProperties();    }    //使用Properties类进行操作    public void testProperties() throws IOException{        //使用Properties类,读取properties文件        Properties pro = new Properties();        //获取class路径下的properties文件        InputStream in =//new FileInputStream("properties.properties")                this.getClass().getClassLoader().getResourceAsStream("properties.properties");        //加载properties文件        pro.load(in);        //获取键-值        Iterator<String> it=pro.stringPropertyNames().iterator();        while(it.hasNext()){            String key=it.next();  //键            System.out.println(key+":"+pro.getProperty(key));//值        }    }}
0 0