Java類集--屬性類Properties
屬性是在程序中經常出現的一種形式。在類集中提供了一個專門的Properties類,以完成屬性的操作。
public class Properties extends Hashtable<Object, Object>
Properties是Hashtable的子類,則也是Map的子類,可以使用Map的全部操作,但是一般情況下屬性類是單獨使用的。

import java.util.Properties;
public class PropertiesDemo01{
public static void main(String args[]){
Properties pro = new Properties() ; // 創建Properties對象
pro.setProperty("BJ","BeiJing") ; // 設置屬性
pro.setProperty("TJ","TianJin") ;
pro.setProperty("NJ","NanJing") ;
System.out.println("1、BJ屬性存在:" + pro.getProperty("BJ")) ;
System.out.println("2、SC屬性不存在:" + pro.getProperty("SC")) ;
System.out.println("3、SC屬性不存在,同時設置顯示的默認值:" + pro.getProperty("SC","沒有發現")) ;
}
};
屬性操作中以上屬於設置和讀取屬性,當然,對於屬性中也可以將屬性保存在文件之中。提供了一下方法:

將以上的屬性寫入到d:\area.properties文件之中。
import java.util.Properties;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class PropertiesDemo02{
public static void main(String args[]){
Properties pro = new Properties() ; // 創建Properties對象
pro.setProperty("BJ","BeiJing") ; // 設置屬性
pro.setProperty("TJ","TianJin") ;
pro.setProperty("NJ","NanJing") ;
File file = new File("D:" + File.separator + "area.properteis") ; // 指定要操作的文件
try{
pro.store(new FileOutputStream(file),"Area Info") ; // 保存屬性到普通文件
}catch(FileNotFoundException e){
e.printStackTrace() ;
}catch(IOException e){
e.printStackTrace() ;
}
}
};
此時,已經將屬性的內容保存在了文件之中。既然可以保存,那麼就可以讀取。

使用以上方法讀取屬性內容:
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class PropertiesDemo03{
public static void main(String args[]){
Properties pro = new Properties() ; // 創建Properties對象
File file = new File("D:" + File.separator + "area.properteis") ; // 指定要操作的文件
try{
pro.load(new FileInputStream(file)) ; // 讀取屬性文件
}catch(FileNotFoundException e){
e.printStackTrace() ;
}catch(IOException e){
e.printStackTrace() ;
}
System.out.println("1、BJ屬性存在:" + pro.getProperty("BJ")) ;
System.out.println("2、SH屬性存在:" + pro.getProperty("SH")) ;
}
};以上是全部保存在了普通的文件之中,實際上在Properties操作的時候也可以將內容全部保存在XML文件之中。
import java.util.Properties;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class PropertiesDemo04{
public static void main(String args[]){
Properties pro = new Properties() ; // 創建Properties對象
pro.setProperty("BJ","BeiJing") ; // 設置屬性
pro.setProperty("TJ","TianJin") ;
pro.setProperty("NJ","NanJing") ;
File file = new File("D:" + File.separator + "area.xml") ; // 指定要操作的文件
try{
pro.storeToXML(new FileOutputStream(file),"Area Info") ; // 保存屬性到普通文件
}catch(FileNotFoundException e){
e.printStackTrace() ;
}catch(IOException e){
e.printStackTrace() ;
}
}
};既然可以使用XML文件格式保存,那麼就可以使用XML文件讀取內容。
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class PropertiesDemo05{
public static void main(String args[]){
Properties pro = new Properties() ; // 創建Properties對象
File file = new File("D:" + File.separator + "area.xml") ; // 指定要操作的文件
try{
pro.loadFromXML(new FileInputStream(file)) ; // 讀取屬性文件
}catch(FileNotFoundException e){
e.printStackTrace() ;
}catch(IOException e){
e.printStackTrace() ;
}
System.out.println("1、BJ屬性存在:" + pro.getProperty("BJ")) ;
}
};
總結:

最後更新:2017-04-03 14:54:43