⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 读取配置文件×.txt

📁 java开发中经常用到的代码
💻 TXT
字号:
从资源文件里读取值的类,文件后缀不一定要.Properties,只要里面内容如:url=www.cnsec.net 
可通过key(url)取得值-www.cnsec.net,简单、强大 


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/**
 * ReadProperties.java
 * Description: 读取操作属性配置文件
 * @author li.b
 * @version 2.0
 * Jun 26, 2008
 */
public class ReadProperties {

    /**
     * Description: 获取属性配置文件
     * @param path 资源文件路径
     * @return    Properties Object
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static Properties getProperties(String path) throws FileNotFoundException, IOException{
        Properties props = null;
        File file = new File(path);
        if(file.exists() && file.isFile()){
            props = new Properties();
            props.load(new FileInputStream(file));
        }else{
            System.out.println(file.toString() + "不存在!");
        }
        return props;
    }
    
    /**
     * Description: 从属性文件获取值
     * @param props Properties Object
     * @param key
     * @return 通过key匹配到的value
     */
    public static String getValue(Properties props,String key,String encod){
        String result = "";
        String en = "";
        String localEN = System.getProperty("file.encoding");
        if(encod !=null && !encod.equals("") ){
            en = encod;
        }else{
            en = localEN;
        }
        try {
            key = new String(key.getBytes(en),"ISO-8859-1");
            result = props.getProperty(key);
            if(!result.equals("")){
                    result = new String(result.getBytes("ISO-8859-1"),en);                
            }
        } catch (Exception e) {
        }finally{
            if(result == null)result = "";
            return result;
        }
    }
    
    public static String getValue(Properties props,String key){
        return getValue(props, key, "");
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -