java屬性檔案properties常用操作工具類,properties工具類

來源:互聯網
上載者:User

java屬性檔案properties常用操作工具類,properties工具類
對於java.util.Properties類,通常我們只需要做到以下3個學習目標:
1、認識properties檔案,理解其含義,會正確建立properties檔案。
2、會使用java.util.Properties類來操作properties檔案。
3、掌握相對路徑,能正確書寫一個properties檔案的相對路徑。
而在平時的工作中,會遇到各種各樣的需求,以下是一個封裝。

package com.herman.util;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;/** * properties 檔案操作工具類 * @author Herman.Xiong * @date 2015-1-29 下午04:27:19 * @version V3.0 * @since jdk 1.6,tomcat 6.0 */public class PropertiesUtil {private static Properties prop = new Properties(); private static InputStream in;private static final String path="src/com/herman/config/conf.properties";/** * 載入 */static{init();}/** * 初始化 */private static void init(){try {in=new FileInputStream(new File(path));prop.load(in);} catch (Exception e) {e.printStackTrace();}}/** * 關閉InputStream * @author Herman.Xiong * @date 2015-1-30 上午09:41:04 */private static void close(){try {if(null != in)in.close(); } catch (Exception e) {e.printStackTrace();}}/** * 重新載入Properties * @author Herman.Xiong * @date 2015-1-30 上午09:41:04 */public static void reload(){close();prop.clear();//清除所有裝載的 鍵 - 值對init();}/** * 刪除所有索引值 * @author Herman.Xiong * @date 2015-1-30 上午09:42:04 */public static void removeAll(){close();File file=new File(path);if(file.exists())file.delete();try {if(!file.exists())new File(path).createNewFile();} catch (IOException e) {e.printStackTrace();}}/** * 輸出所有內容到控制台 * @author Herman.Xiong * @date 2015-1-30 上午09:42:33 */public static void outList(){prop.list(System.out);}public static void main(String[] args) {System.out.println(prop.getProperty("abc"));outList();reload();outList();removeAll();System.out.println("重新載入.....");outList();}}

歡迎大家關注我的部落格!如有疑問,請加QQ群:135430763共同學習!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.