读取properties配置文件

来源:互联网 发布:阿里云怎么退款 编辑:程序博客网 时间:2024/06/11 14:57

有的时候我们需要把一些url信息保存在一个properties文件中。方便统一修改和管理。

这时就需要一个类文件来读取properties的内容。ps:properties文件一般放在resource目录下。代码如下:


import java.io.*;import java.util.Properties;/** * Created by Administrator on 2016/4/23. */public class PropertiesUtil {    private String propertiesName="";//properties文件的名称    public PropertiesUtil() {    }    public PropertiesUtil(String propertiesName) {//有参构造,创建这个对象的时候就把properties文件的名称带入        this.propertiesName = propertiesName;    }<span style="white-space:pre"></span>//这是读取方法。    public String readProperty(String key){        String value="";        InputStream is=null;        try {            is=PropertiesUtil.class.getClassLoader().getResourceAsStream(propertiesName);            Properties p=new Properties();            p.load(is);            value=p.getProperty(key);        } catch (IOException e) {            e.printStackTrace();        }finally {            try {                is.close();            } catch (IOException e) {                e.printStackTrace();            }        }        return value;    }//这时获得properties对象的方法。    public Properties getProperties(){        Properties p=new Properties();        InputStream is=null;        try{            is=PropertiesUtil.class.getClassLoader().getResourceAsStream(propertiesName);            p.load(is);        }catch (Exception e){            e.printStackTrace();        }finally{            try {                is.close();            } catch (IOException e) {                e.printStackTrace();            }        }        return p;    }//写入properties键值对的方法    public void writeProperty(String key,String value){        InputStream is=null;        OutputStream os=null;        Properties p=new Properties();        try {            is=new FileInputStream(propertiesName);            p.load(is);            os=new FileOutputStream(Properties.class.getClassLoader().getResource(propertiesName).getFile());            p.setProperty(key,value);            p.store(os,key);            os.flush();            os.close();        } catch (Exception e) {            e.printStackTrace();        }finally {            try {                if(null!=is){                    is.close();                }                if(null !=os){                    os.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }    }    public static void main(String[] args) {        PropertiesUtil p=new PropertiesUtil("server.properties");//创建一个工具类对象        Properties ps=p.getProperties();//得到properties对象。        System.out.println(ps.getProperty("mp"));//根据键得到值。    }}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

properties文件内容:

mp=zhangsan

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



0 0