Java读写Properties文件及JavaIO中字节流和字符的转换

本文介绍如何在Java中读写Properties文件,包括使用Properties类进行操作的实例代码。同时,探讨了字节流到字符的转换过程,详细解析了StreamDecoder的核心方法。
摘要由CSDN通过智能技术生成

读写properties文件

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

    /**
     * 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文件:

    /**
     * 
     */
    private void setPa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值