1. 程式人生 > >java中Properties類及讀取properties中屬性值

java中Properties類及讀取properties中屬性值

key ioe failed .cn pre new ava 進行 html

在項目的應用中,經常將一些配置放入properties文件中,在代碼應用中讀取properties文件,就需要專門的類Properties類,通過這個類可以進行讀取。

深入理解和學習的參考的詳見:深入理解和學習Properties參考

此處展現在項目中讀取properties配置文件中的幫助類,代碼可以直接使用:

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

public class PropertiesUtil
{

    public static final String FILE_PATH = "properties/upload.properties";

    //通過傳入的路徑及key,獲得對應的值
    
public static String getValue(String path, String key) { Properties properties = new Properties(); try { properties.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(path)); } catch (IOException e) { throw new RuntimeException("File Read Failed...", e); }
return properties.getProperty(key); } //通過key直接獲取對應的值 public static String getValue(String key) { Properties properties = new Properties(); try { properties.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(FILE_PATH)); }
catch (IOException e) { throw new RuntimeException("File Read Failed...", e); } return properties.getProperty(key); } }

java中Properties類及讀取properties中屬性值