配置文件使用

java中提供了Properties类,来读取properties配置文件中保存的相关参数

常用的方法

来自菜鸟教程

//用指定的键在此属性列表中搜索属性
String getProperty(String key)

//用指定的键在属性列表中搜索属性。
String getProperty(String key, String defaultProperty)

//将属性列表输出到指定的输出流
void list(PrintStream streamOut)
void list(PrintWriter streamOut)

// 从输入流中读取属性列表(键和元素对)。
void load(InputStream streamIn) throws IOException

//按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
Enumeration propertyNames( )

//调用 Hashtable 的方法 put。
Object setProperty(String key, String value)

//以适合使用  load(InputStream)方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表键和元素对)写入输出流
void store(OutputStream streamOut, String description)

下面是java中properties使用示范

在src目录下创建properties文件config.properties

##注释 属性值
user=name
password=123456

创建辅助类PropertiesUtil,这里默认的路径根节点与辅助类所在包名一致,如果不在同一目录下,需要通过/切换根节点

public class PropertiesUtil {
    //配置文件路径
    private  String configPath="config.properties";

    //配置文件对象
    private Properties pros=null;

    //默认构造函数,自动找到classPath路径下的config.properties
    public PropertiesUtil() throws IOException {
        InputStream in=PropertiesUtil.class.getResourceAsStream(configPath);
        pros=new Properties();
        pros.load(in);
        if (in != null) {
            in.close();
        }
    }

    /**
     * 根据key读取配置的值
     * @param key
     * @return
     */
    public String getValueByKey(String key){
        return pros.getProperty(key);
    }

    /**
     * 读取配置文件中的所有信息
     * @return
     */
    public Map<String,String> getAllProperties(){
        Map<String,String> map=new HashMap<>();
        Enumeration en=pros.propertyNames();
        while (en.hasMoreElements()){
            String key=(String) en.nextElement();
            String value=pros.getProperty(key);
            map.put(key,value);
        }
        return map;
    }

    public void setValue(String key,String value) throws IOException {
        Properties properties=new Properties();
        InputStream ins=new FileInputStream(this.configPath);
        //加载文件,获取属性列表
        properties.load(ins);
        // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
        // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
        OutputStream fos = new FileOutputStream(this.configPath);
        properties.setProperty(key, value);
        // 以适合使用 load 方法加载到 Properties 表中的格式,
        // 将此 Properties 表中的属性列表(键和元素对)写入输出流
        properties.store(fos,"last update");

        ins.close();
        fos.close();
    }

}

测试辅助类

public class Main {

    public static void main(String[] args) {
	// write your code here
        PropertiesUtil util= null;
        try {
            util = new PropertiesUtil();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //读取所有属性
            Map<String,String> map=util.getAllProperties();

            //foreach
            for (Map.Entry<String, String> entry:
                    map.entrySet()){
                String result="key="+entry.getKey()+",value="+entry.getValue();
                System.out.println(result);
            }

    }
}

在android中properties的应用

首先,properties文件放在assets目录下,通过assets打开

Properties props=new Properties();

//在android中打开properties文件,读取配置
InputStream ins=getAssets().open("config.properties");
//String configPath="/assets/config.properties";
//InputStream ins=PropertiesUtil.class.getResourceAsStream(configPath);
pros.load(ins);

//获取属性
props.getProperty("user","name");

//修改/插入属性
props.setProperty("user","name1");

//添加新的注释
OutputStream output=openFileOutput("config.properties",MODE_PRIVATE);
props.put("time","1");
props.store(output,"last update");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值