Java Properties类的常规使用

         在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类的常用操作,望批评指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值