properties

读写properties文件

Java读写Properties文件是一个比较常见的需求,一般的做法是将properties文件读到Properties类对象中,通过Properteis对象来操作。下面是一段实例代码:

  1.   /** 
  2.    * Read Properties file with ASCII codes only 
  3.    */  
  4. ublic static Properties getProperties(String fileName, String path){  
  5.     Properties props = new Properties();  
  6.       
  7.     InputStream in = null;  
  8. try {  
  9.     in = new FileInputStream(path + fileName);  
  10. catch (FileNotFoundException e1) {  
  11.     System.out.println("Can't find c3p0.properties");  
  12. }  
  13.   
  14.     try {  
  15.     props.load(in);  
  16.     in.close();  
  17. catch (IOException e) {  
  18.     System.out.println("Can't load c3p0.properties");  
  19. }             
  20.   
  21. return props;  
  22.   }  
    /**
     * Read Properties file with ASCII codes only
     */
 public static Properties getProperties(String fileName, String path){
    	Properties props = new Properties();
    	
    	InputStream in = null;
		try {
			in = new FileInputStream(path + fileName);
		} catch (FileNotFoundException e1) {
			System.out.println("Can't find c3p0.properties");
		}
		
    	try {
			props.load(in);
			in.close();
		} catch (IOException e) {
			System.out.println("Can't load c3p0.properties");
		}    	   	
		
		return props;
    }
上面的代码是用于读取仅包含ASCII码的properties文件,特点是只用了FileInputStream,而没有像往常一样在外面套个FileReader。下面的代码用于写ASCII编码的properties文件:

  1.   /** 
  2.    *  
  3.    */  
  4.   private void setPassword(String passWord){  
  5.     Properties props = DBUtil.getC3P0Properties();  
  6.     FileOutputStream out;  
  7. try {  
  8.     String path = DBUtil.getFullPath(this.getClass());  
  9.     out = new FileOutputStream(path + "/c3p0.properties" );  
  10.             props.setProperty("c3p0.password", passWord);  
  11.             props.store(out, "Prevent connect for failed connection");  
  12.             out.close();  
  13. catch (FileNotFoundException e) {  
  14.     // TODO Auto-generated catch block  
  15.     e.printStackTrace();  
  16. catch (IOException e) {  
  17.     // TODO Auto-generated catch block  
  18.     e.printStackTrace();  
  19. }  
  20.   }  
    /**
     * 
     */
    private void setPassword(String passWord){
    	Properties props = DBUtil.getC3P0Properties();
    	FileOutputStream out;
		try {
			String path = DBUtil.getFullPath(this.getClass());
			out = new FileOutputStream(path + "/c3p0.properties" );
	    	        props.setProperty("c3p0.password", passWord);
	    	        props.store(out, "Prevent connect for failed connection");
	    	        out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

字节流到字符的转换

关于Java I/O的更全面信息,可以参考Developerworks上的这篇文章: http://www.ibm.com/developerworks/cn/java/j-lo-javaio/

这里贴一下StreamDecoder中的核心方法,看看StreamDecoder是怎样将Stream转为Character的吧:

  1. private int read0() throws IOException {  
  2.       synchronized (lock) {  
  3.   
  4.           // Return the leftover char, if there is one  
  5.           if (haveLeftoverChar) {  
  6.               haveLeftoverChar = false;  
  7.               return leftoverChar;  
  8.           }  
  9.   
  10.           // Convert more bytes  
  11.           char cb[] = new char[2];  
  12.           int n = read(cb, 02);  
  13.           switch (n) {  
  14.           case -1:  
  15.               return -1;  
  16.           case 2:  
  17.               leftoverChar = cb[1];  
  18.               haveLeftoverChar = true;  
  19.               // FALL THROUGH  
  20.           case 1:  
  21.               return cb[0];  
  22.           default:  
  23.               assert false : n;  
  24.               return -1;  
  25.           }  
  26.       }  
  27.   }  
  private int read0() throws IOException {
        synchronized (lock) {

            // Return the leftover char, if there is one
            if (haveLeftoverChar) {
                haveLeftoverChar = false;
                return leftoverChar;
            }

            // Convert more bytes
            char cb[] = new char[2];
            int n = read(cb, 0, 2);
            switch (n) {
            case -1:
                return -1;
            case 2:
                leftoverChar = cb[1];
                haveLeftoverChar = true;
                // FALL THROUGH
            case 1:
                return cb[0];
            default:
                assert false : n;
                return -1;
            }
        }
    }

从上面的代码可以看出,StreamDecoder每次读入两个byte,然后逐个字节进行解析。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值