博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
properties
阅读量:3590 次
发布时间:2019-05-20

本文共 3621 字,大约阅读时间需要 12 分钟。

读写properties文件

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

[java]
  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文件:

[java]
  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上的这篇文章:

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

[java]
  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,然后逐个字节进行解析。

转载地址:http://phswn.baihongyu.com/

你可能感兴趣的文章
【c语言】蓝桥杯算法提高 三个整数的排序
查看>>
【c语言】蓝桥杯算法提高 P0101
查看>>
【c语言】统计字符次数
查看>>
CTDB原理介绍
查看>>
CTDB配置文件参数解析
查看>>
利用NFS共享搭建CTDB集群
查看>>
Python面向对象编程
查看>>
MAN QPIDD
查看>>
python import自定义模块
查看>>
Python利用XMLRPC实现分布式系统
查看>>
执行yum提示错误:rpmdb: BDB0113 Thread/process 424227/139826856310848 failed
查看>>
proftpd服务器搭建
查看>>
ProFTPD:Limit配置
查看>>
IDEA恢复布局
查看>>
重定向和请求转发的区别
查看>>
Map、Set、List集合区别(看完秒懂)
查看>>
普通用户使用docker命令遇到提示需要提升权限时的解决方法
查看>>
webpack打包技术
查看>>
Leecode 面试题09用两个栈实现队列
查看>>
fastdfs连接超时报错解决方案
查看>>