读取.properties文件

环境:eclipse
步骤一:
创建Maven项目
步骤二:
创建PropertiesUtil类:

package Tool;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil{

    //输出name属性文件内容
    public static void outputvalusofProperty(String name) throws IOException{
        InputStream in=ClassLoader.getSystemResourceAsStream(name);
        Properties pro =new Properties();
        pro.load(in);
        
        for (String key:pro.stringPropertyNames()){
            System.out.println("key:"+key+" "+"value:"+pro.getProperty(key));
        }
    }
    
    //获取filename属性文件中,关键字为key的值
    public static String getProperty(String filename,String key) throws IOException{
        InputStream in=ClassLoader.getSystemResourceAsStream(filename);
        Properties pro =new Properties();
        pro.load(in);
        
        if(pro.containsKey(key))
            return pro.getProperty(key);
        else 
            return null;
    }
}

你可能感兴趣的:(读取.properties文件)