Java讀取Property配置文件
經常需要讀取Property配置文件,就寫了個工具類,主要還是使用java.util.Properties, 簡單封裝了一下。
Java讀取Property文件:
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; /** * 讀取properties文件工具類 */ public class PropertiesUtil { /** * 獲取指定路徑下property文件中的某個字段(沒有默認值) * @param key 字段 * @param filePath 屬性文件 * @return 與key對應的value * @throws FileNotFoundException if property file doesn't exists * @throws IOException if there is some exception when load from property file */ public static String getProperties(String key,String filePath) throws FileNotFoundException, IOException{ File file = new File(filePath); Properties props = new Properties(); props.load(new FileInputStream(file)); return props.getProperty(key); } /** * 獲取指定路徑下property文件中的某個字段(可以設置默認值) * @param key 字段 * @param value 默認值 * @param filePath 屬性文件 * @return 與key對應的value * @throws FileNotFoundException if property file doesn't exists * @throws IOException if there is some exception when load from property file */ public static String getProperties(String key,String defaultValue,String filePath) throws FileNotFoundException, IOException{ File file = new File(filePath); Properties props = new Properties(); props.load(new FileInputStream(file)); return props.getProperty(key,defaultValue); } }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!