java.util.Properties與提取Windows壁紙小程序的改進

propertiesHashTable的子類,具有Map集合的特點,其鍵和值都是字符串;同時也是集合中與IO技術(shù)相結(jié)合的容器

Properties p=new Properties();
p.setProperty("ziti", "songti");
p.setProperty("yanse", "hongse");
System.out.println(p);//{ziti=songti, yanse=hongse}
System.out.println(p.getProperty("yanse"));//hongse

基本方法

  • setProperty(String key, String value)
    調(diào)用 Hashtable 的方法 put
    getProperty(String key)
    用指定的鍵在此屬性列表中搜索屬
    list(PrintStream out)
    將屬性列表輸出到指定的輸出流(如果輸出流是System.out的話與System.out.println(p)的效果類似)
BufferedReader br=new BufferedReader(new FileReader("F:\\properties.txt"));
            Properties p=new Properties();
            p.load(br);
            p.setProperty("font", "songti");
            BufferedWriter bw=new BufferedWriter(new FileWriter("F:\\properties.txt"));
            p.store(bw, "huohuo");
            br.close();
            bw.close();

與io技術(shù)結(jié)合的部分

  • load(InputStream inStream)
    從輸入流中讀取屬性列表(鍵和元素對)
  • store(OutputStream out, String comments)
    以適合使用 load(InputStream) 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流

提取Windows壁紙(舊)

public class 從電腦中提取Windows壁紙 {

    public static void main(String[] args) throws IOException{
        File source = new File("C:\\Users\\DELL\\AppData\\Local\\Packages\\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\\LocalState\\Assets");
        File mudi = new File("F:\\鎖屏圖片");
        File[] files = source.listFiles();
            int index = Integer.parseInt(new BufferedReader(new FileReader("F:\\鎖屏圖片\\圖片信息.txt")).readLine());
            for (File f : files) {
                if (f.isDirectory()) {
                    continue;
                }
                String newfName = "F:\\鎖屏圖片\\圖片__" + index + ".jpg";
                copy(f.getPath(), newfName);
                index++;
            }
            FileWriter filein = new FileWriter("F:\\鎖屏圖片\\圖片信息.txt");
            filein.write(Integer.toString(index));
            filein.close();
    }

    public static void copy(String oldFilePath, String newFilePath) throws IOException {
        BufferedInputStream oldf = new BufferedInputStream(new FileInputStream(oldFilePath));
        BufferedOutputStream newf = new BufferedOutputStream(new FileOutputStream(newFilePath));
        int ch;
        while ((ch = oldf.read()) != -1) {
            newf.write(ch);
        }
        oldf.close();
        newf.close();
    }
}

缺點:可讀性差,不夠方便

提取Windows壁紙(新)

public class 提取壁紙plus {

    public static void main(String[] args) throws IOException{
        File source = new File("C:\\Users\\DELL\\AppData\\Local\\Packages\\Microsoft."
                + "Windows.ContentDeliveryManager_cw5n1h2txyewy\\LocalState\\Assets");
        File mudi = new File("F:\\鎖屏圖片");
        File[] files = source.listFiles();
        Properties info=new Properties();
        BufferedReader br=new BufferedReader(new FileReader("F:\\鎖屏圖片\\圖片信息.txt"));
        info.load(br);
            int index = Integer.parseInt(info.getProperty("index"));
            for (File f : files) {
                if (f.isDirectory()) {
                    continue;
                }
                String newfName = "F:\\鎖屏圖片\\圖片__" + index + ".jpg";
                copy(f.getPath(), newfName);
                index++;
            }
            FileWriter filein = new FileWriter("F:\\鎖屏圖片\\圖片信息.txt");
            info.setProperty("index", index+"");
            info.store(filein, "Picture Index");
            br.close();
            filein.close();
    }

    public static void copy(String oldFilePath, String newFilePath) throws IOException {
        BufferedInputStream oldf = new BufferedInputStream(new FileInputStream(oldFilePath));
        BufferedOutputStream newf = new BufferedOutputStream(new FileOutputStream(newFilePath));
        int ch;
        while ((ch = oldf.read()) != -1) {
            newf.write(ch);
        }
        oldf.close();
        newf.close();
    }
}

雖然看起來并沒有簡化代碼...但是提高了代碼的閱讀性,特別是當(dāng)屬性比較多的時候使用Properties對象就既簡潔又方便

?著作權(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ù)。

相關(guān)閱讀更多精彩內(nèi)容

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