1、掌握Properties 类的使用
2、可以普通及XML 格式的文件中保存及读取属性
属性是在程序中经常出现的一种形式。
在类集中提供了一个专门的Properties 类,以完成属性的操作。
public class Properties extends Hashtable<Object, Object>
Properties 是Hashtable 的子类,则也是Map 的子类,可以使用Map 的全部操作,但是一般情况下属性类是单独使用的。
设置和取得属性:
设置属性:
public Object setProperty(String key, String value)
得到属性:
public String getProperty(String key)
public String getProperty(String key, String defaultValue)
验证以上的操作方法:
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","没有发现")) ;
}
};
属性操作中以上属于设置和读取属性,当然,对于属性中也可以将属性保存在文件之中。提供了以下的方法:
public void Store(OutputStream out, String comments) throws IOException
将以上的属性写入到 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() ;
}
}
};
此时,已经将属性的内容保存在了文件之中。既然可以保存,那么就可以读取。
public void load(InputStream inStream) throws IOException
使用以上方法读取属性内容
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")) ;
}
};
总结:
1、如果要进一步了解属性操作,则可以继续学习后续的反射机制部分,了解属性类的应用
2、属性中的类型肯定都是字符串,因为操作最方便。
3、属性可以向普通文件或XML 文件中保存或读取,按照指定格式可以向文件中任意扩充属性