Java21-4 IO技術(shù) ProPerties集合

特點:
1.Hashtable子類,map集合中的方法都可以使用 所以存在鍵 值
2.該集合沒有泛型。 屬性列表中的每個鍵及其對應(yīng)的值都是一個字符串。
3.他是一個可以持久化的屬性集。鍵值可以儲存到集合中,也可以存儲到持久化設(shè)備上
鍵值的來源也可以來源于一個持久化設(shè)備
4.java設(shè)置文件語意性強 .properties后綴名
存儲復雜數(shù)據(jù)常用為.xml
5.寫入各項目自動刷新輸出流 .store()方法返回后 輸出流仍然保持打開狀態(tài)
所以要自己手動關(guān)閉流

因為Properties從繼承Hashtable時, put和putAll方法可應(yīng)用于Properties對象。 強烈不鼓勵使用它們,因為它們允許調(diào)用者插入其鍵或值不是Strings 。應(yīng)該使用setProperty方法。 如果store或save方法在包含非String鍵或值的“受損害” Properties對象上調(diào)用,則調(diào)用將失敗。 類似地,如果在包含非String密鑰的“受損害” Properties對象上調(diào)用propertyNames或list方法的調(diào)用將失敗。

常用方法 差不多都用@-@

示例1 將集合中的值存入set集合 及通過IO技術(shù)傳入文件

public class Properties_Test1 {
    public static void main(String[] args) throws IOException {
    methodDemo();
    }
    public static void methodDemo() throws IOException {
        Properties prop=new Properties();
//        設(shè)置鍵和值
        prop.setProperty("zhangsan","20");
        prop.setProperty("lisi","21");
//通過set集合存儲鍵 通過getProperty(); 獲取值 同Map
        Set<String> set=prop.stringPropertyNames();
        for (String s:set) {
            prop.getProperty(s);
        }

        FileOutputStream fos=null;
//        持久化 存儲到設(shè)備中 需要輸出流 store(OutputStream out, String comments)
//        設(shè)置輸出文件位置 和文件備注
        prop.store(fos=new FileOutputStream("D:\\IO\\info.properties"),"person.info");
 /*         寫入各項目自動刷新輸出流 .store()方法返回后 輸出流仍然保持打開狀態(tài)
    所以要自己手動關(guān)閉流*/
        fos.close();

    }
}

示例2 讀取文件流中數(shù)據(jù)

public class Properties_Test2 {

    public static void main(String[] args) throws IOException {
        methodDemo();
    }
    public static void methodDemo() throws IOException {
        Properties prop=new Properties();

// 定義讀取流和數(shù)據(jù)文件關(guān)聯(lián)
        File file=new File("D:\\IO\\info.properties");
        FileInputStream fis=new FileInputStream(file);
//       將數(shù)據(jù)加載到Properties集合中
        prop.load(fis);
//        鍵相同可以替換值
        prop.setProperty("lisi","12");
//        這樣只改變內(nèi)存 還需要重新持久化
        FileOutputStream fos=new FileOutputStream(file);
        prop.store(fos,"");
//        將集合中的元素 存儲到設(shè)備中
        fis.close();
        fos.close();

    }
}

實驗 定義一個功能記錄程序運行次數(shù) 滿5次以后 給出提示 試用結(jié)束 請注冊

public class Test {
    public static void main(String[] args) throws IOException {
        boolean b=checkCount();
        if (b)
            run();
    }

    private static void run() {
        System.out.println("程序運行");
    }

    private static boolean checkCount() throws IOException {
        File file=new File("D:\\IO\\info1.Properties");
        //創(chuàng)建文件
        if(!file.exists())
            file.createNewFile();
        //記錄每次存儲的次數(shù)
        int count=0;
        Properties prop=new Properties();
        FileInputStream fis=new FileInputStream(file);
        //將數(shù)據(jù)加載到集合中
        prop.load(fis);
//        獲取鍵次數(shù)
        String value=prop.getProperty("count");
//        如果count鍵的值不為空 則將值付給count
        if (value!=null) {
             count=Integer.parseInt(value);

        }
        //自增 自增后重新存儲到集合中 第一次count=0 運行自增為1
        count++;
//        將count的值重新寫入
        prop.setProperty("count",String.valueOf(count));
//        重新持久化
        FileOutputStream fos=new FileOutputStream(file);
        prop.store(fos,"cishu");

          fis.close();
          fos.close();
        if (count>5) {
            System.out.println("請注冊");
            return false;
        }
        return true;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容