通过java读取.properties 配置文件

 WEB-INF/classes/number.properties 配置文件:

#还款期限45天
repay_deadline=45

 java读取配置文件:

package com.tcwl.vsmp.utils;

import java.io.*;
import java.util.Properties;

/** 
* Commons Configuration读取属性文件的例子 
* 
*/
public class BatchNumber
{
    /**
     * 
     */
    private static String configPath = BatchNumber.class.getResource(
            "/number.properties").getFile();
    
    /**
     * 
     */
    private static String numAddPath = BatchNumber.class.getResource(
            "/numAdd.properties").getFile();
    
    


    public synchronized  static void setTerminateNm(){
        Properties prop = new Properties();
        InputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(
                    numAddPath));
            prop.load(in); // /加载属性列表

            prop.setProperty("serialNum", ""+1);
            FileOutputStream outputFile = new FileOutputStream(numAddPath);
            prop.store(outputFile, "modify");
            outputFile.flush();
            outputFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   
    /**
     * 得到一个字符串,是一个递增的数字,每调用一次增加1
     */
    public synchronized static String getNumberValue(String key)
    {
        Properties prop = new Properties();
        String str = "";
        //String str1 = "";
        try
        {
            // 读取属性文件a.properties
            InputStream in = new BufferedInputStream(new FileInputStream(
                    numAddPath));
            prop.load(in); // /加载属性列表


            //Integer num = Integer.valueOf(prop.getProperty("numadd"));
            Integer num = Integer.valueOf(prop.getProperty(key));

            if(key.equals("numadd")){

                str = String.format("%08d", num);

                num = (num >= 99999999) ? 1 : num + 1;

            }else{
                str = String.valueOf(num);
                num = num + 1;
            }
            System.out.println(str);
            //in.
            in.close();
            //prop.setProperty("numadd", ""+num);
            prop.setProperty(key, ""+num);
            FileOutputStream outputFile = new FileOutputStream(numAddPath);
            prop.store(outputFile, "modify");  
            outputFile.flush();  
            outputFile.close();  

        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println(e);
        }
        return str;
    }




    /**
     * 
     * @param key key
     * @return 结果
     */
    public static String getValue(String key)
    {
        Properties prop = new Properties();
        String value = "";
        try
        {
            // 读取属性文件a.properties
            InputStream in = new BufferedInputStream(new FileInputStream(
                    configPath));
            prop.load(in); // /加载属性列表


            value = prop.getProperty(key);
            in.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println(e);
        }
        return value;
    }

    /**
     * 
     * @param args 参数
     */
    public static void main(String[] args)
    {
        // // 当前类的绝对路径
        // String cp = "/number.properties";
        // System.out.println(BatchNumber.class.getResource("/").getFile());
        // // 指定CLASSPATH文件的绝对路径


        //
        // System.out.println(BatchNumber.class.getResource(cp).getFile());
        // // 指定CLASSPATH文件的绝对路径


        //
        // File f = new File(BatchNumber.class.getResource(cp).getFile());
        // System.out.println(f.getPath());
        // System.out.println(BatchNumber.getValue());
    }

    /**
     * 设置某个key的值,并保存至文件.
     * @param key key值


     * @param value 值


     * @throws IOException
     */
    public void setValue(String key, String value) throws IOException
    {
        Properties prop = new Properties();
        InputStream fis = new FileInputStream(this.configPath);
        // 从输入流中读取属性列表(键和元素对)
        prop.load(fis);
        // System.out.println(prop.get(key));
        // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。


        // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。


        OutputStream fos = new FileOutputStream(this.configPath);
        prop.setProperty(key, value);
        // 以适合使用 load 方法加载到 Properties 表中的格式,
        // 将此 Properties 表中的属性列表(键和元素对)写入输出流


        prop.store(fos, "last update");
        // 关闭文件
        fis.close();
        fos.close();
    }
}

 java定义常量:

package com.tcwl.vsmp.common;

/**
 * OilConstant
 *
 */
public class OilConstant
{


    /**
     * 还款期限40天
     */
    public static final String REPAY_DEADLINE = "repay_deadline";


}

通过常量数据 获取配置文件数据:

        /**
         * 还款期限45天
         */
        Integer REPAY_DEADLINE =Integer.valueOf(BatchNumber
                 .getValue(OilConstant.REPAY_DEADLINE));

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_无往而不胜_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值