在Java中,Properties文件表示配置文件,文件類型爲*.Properties,文件內容以鍵值對的形式存放文本信息。其中Properties文件中“#”表示註釋信息。
1、Java通過Properties類將內容寫入Properties文件
import java.util.Properties; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.BufferedOutputStream; public class PropertiesDemo01{ public static void main(String[] args){ // 聲明變量,開闢佔內存空間 Properties p=null; OutputStream output=null; File f=null; try{ // 實例化對象,開闢對內存空間 p=new Properties(); f=new File("d:"+File.separator+"demo.properties"); // 使用帶緩衝區的輸出流 output=new BufferedOutputStream(new FileOutputStream(f,true)); // 設置價值對 省份-省會 p.setProperty("sc","成都"); p.setProperty("sd","濟南"); p.setProperty("hb","石家莊"); // 存儲至文件內 p.store(output,"測試"); }catch(FileNotFoundException ex){ ex.printStackTrace(); }catch(IOException ex){ ex.printStackTrace(); }finally{ try{ if(output!=null){ output.close(); } }catch(IOException ex){ } } } }
執行結果如下:
在結果中我們看到了編碼後的鍵值對。Java中給我們提供了native2ascii.exe工具可以實現將文本類文件編碼轉換爲Unicode編碼。native2ascii.exe工具使用截圖如下:
2、通過Properties類讀取Properties文件
import java.util.Properties; import java.util.Enumeration; import java.io.InputStream; import java.io.FileInputStream; import java.io.BufferedInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; public class PropertiesDemo02{ public static void main(String[] args){ // 聲明變量,開闢佔內存空間 File f=null; InputStream input=null; Properties pro=null; String key=null; String value=null; try{ // 實例化變量,開闢對內存空間 f=new File("d:"+File.separator+"demo.properties"); input=new BufferedInputStream(new FileInputStream(f)); pro=new Properties(); // 加載Properties配置文件 pro.load(input); Enumeration<?> enu=pro.propertyNames(); while(enu.hasMoreElements()){ key=String.valueOf(enu.nextElement()); value=pro.getProperty(key); System.out.println(String.format("鍵:%s->值:%s",key,value)); } }catch(FileNotFoundException ex){ ex.printStackTrace(); }catch(IOException ex){ ex.printStackTrace(); }finally{ try{ if(input!=null){ input.close(); } }catch(IOException ex){ ex.printStackTrace(); } } } }
執行結果如下:
3、使用Properties類生成XMl文件
import java.util.Properties; import java.io.File; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.IOException; public class PropertiesDemo03{ public static void main(String[] args){ // 聲明變量,開闢棧內存空間 File f=null; OutputStream output=null; Properties pro=null; try{ // 實例化變量,開闢對內存空間 f=new File("d:"+File.separator+"Area.xml"); output=new BufferedOutputStream(new FileOutputStream(f,true)); pro=new Properties(); // 設置鍵值對 簡稱-用戶名 pro.setProperty("zs","張三"); pro.setProperty("ls","李四"); pro.setProperty("sq","孫琦"); // 存儲爲XML文件 pro.storeToXML(output,"簡稱-用戶姓名"); }catch(FileNotFoundException ex){ ex.printStackTrace(); }catch(IOException ex){ ex.printStackTrace(); }finally{ try{ if(output!=null){ output.close(); } }catch(IOException ex){ ex.printStackTrace(); } } } }
執行結果如下:
4、使用Properties類讀取生成的XML文件
import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import java.io.BufferedInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import java.util.Iterator; import java.util.Map; public class PropertiesDemo04{ public static void main(String[] args){ // 聲明變量,開闢棧內存空間 File f=null; InputStream input=null; Properties pro=null; Iterator<Map.Entry<Object,Object>> iterator=null; Map.Entry<Object,Object> entry=null; String key=null; String value=null; try{ // 實例化變量,開闢對內存空間 f=new File("d:"+File.separator+"Area.xml"); input=new BufferedInputStream(new FileInputStream(f)); pro=new Properties(); // 讀取Xml文件信息 pro.loadFromXML(input); // 輸出XML文件內容 iterator=pro.entrySet().iterator(); while(iterator.hasNext()){ entry=iterator.next(); key=String.valueOf(entry.getKey()); value=String.valueOf(entry.getValue()); System.out.println(String.format("鍵:%s->值:%s",key,value)); } }catch(FileNotFoundException ex){ ex.printStackTrace(); }catch(IOException ex){ ex.printStackTrace(); }finally{ try{ if(input!=null){ input.close(); } }catch(IOException ex){ ex.printStackTrace(); } } } }
執行結果如下:
以上是本人整理的關於Properties類的常用操作,望批評指正!