properties是HashTable的子類,具有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對象就既簡潔又方便