读取配置文件

//////////////////////使用////////////////
/*
PropertyReaderUtil prUtil = new PropertyReaderUtil("./config/env/app.properties");
String url0 = prUtil.getValue("strorageurl");
*/

package com.cib.doc.util;

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

public class PropertyReaderUtil {

private Properties propertie;
    private FileInputStream inputFile;
    private InputStream inputStream;
    private FileOutputStream outputFile;
  
  
    public PropertyReaderUtil(String filename){//filename:配置文件路径PropertyReaderUtil prUtil = new PropertyReaderUtil("./config/env/app.properties");
        propertie = new Properties();
        try {
        inputStream = this.getClass().getClassLoader().getResourceAsStream(filename);
            propertie.load(inputStream);
            inputStream.close();
        } catch (FileNotFoundException ex) {
            System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
            ex.printStackTrace();
        } catch (IOException ex){
            System.out.println("装载文件--->失败!");
            ex.printStackTrace();
        }
    }
  
    public String getValue(String key){//配置文件的属性值String url0 = prUtil.getValue("strorageurl");
        if(propertie.containsKey(key)){
            String value = propertie.getProperty(key);//得到某一属性的值
            return value;
        }
        else
            return "";
    }
   
    public String getValue(String fileName, String key){
        try{
            String value = "";
            inputFile = new FileInputStream(fileName);
            propertie.load(inputFile);
            inputFile.close();
            if(propertie.containsKey(key)){
                value = propertie.getProperty(key);
                return value;
            }else
                return value;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "";
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } catch (Exception ex) {
            ex.printStackTrace();
            return "";
        }
    }
   
    public void clear()
    {
        propertie.clear();
    }
  
  
    public void setValue(String key, String value)
    {
        propertie.setProperty(key, value);
    }
     
    public void saveFile(String fileName, String description)
    {
        try {
            outputFile = new FileOutputStream(fileName);
            propertie.store(outputFile, description);
            outputFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ioe){
            ioe.printStackTrace();
        }
    }
  
    public static void main(String[] args)
    {
    String filename = "./config/env/app.properties";
   
    PropertyReaderUtil rc = new PropertyReaderUtil(filename);
      
        String ip = rc.getValue("strorageurl");//以下读取properties文件的值
      
        System.out.println("ip = " + ip );//以下输出properties读出的值
          
      
      
    }
  
}

你可能感兴趣的:(读取配置文件)