读propert文件

PropertiesUtil.java

package utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {
    private static InputStream in;//=prop.class.getClassLoader().getResourceAsStream("resource/date.properties");
    private static Properties props;
    private static String filePath=PropertiesUtil.class.getClassLoader().getResource("resource/date.properties").getFile();//.getPath();
    static{
        props=new Properties();
        
            try {
                in=new BufferedInputStream(new FileInputStream(filePath));
                props.load(in);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();
            }
    }
    public static String readStringValue(String key) {
        return props.getProperty(key);
    }
    
    public static int readIntValue(String key) {
        return Integer.valueOf(props.getProperty(key));
    }
    
    public static Float readFloatValue(String key) {
        return Float.valueOf(props.getProperty(key));
    }
    
    public static String[] readStringArray(String key,String regex) {
        return props.getProperty(key).split(regex);
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(PropertiesUtil.readStringValue("aa"));
        System.out.println(PropertiesUtil.readIntValue("age"));
        System.out.println(PropertiesUtil.readFloatValue("fl"));
        System.out.println(PropertiesUtil.readStringArray("aa",",")[1]);
    }

}

 

date.properties

#用来测试的
aa=1,2,3,4,54,54,5,43,5,32,3,4532,45
age=1
fl=1.1

  public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();   
     }

String.format("%03d",Integer.valueOf(no2)+1);

 

 

posted @ 2013-06-19 10:48  墙头一颗草  Views(292)  Comments(0Edit  收藏  举报