属性是在程序中经常出现的一种形式。
Properties是Hashtable的子类,则也是Map的子类,可以使用Map的全部操作,但是一般情况下属性类是单独使用的。
属性操作中以上属于设置和读取属性,当然,对于属性中也可以将属性保存在文件之中。提供了一下方法:
此时,已经将属性的内容保存在了文件之中。既然可以保存,那么就可以读取。
在类集中提供了一个专门的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")) ;
}
};