Java解析Unicode格式文件

在多语言环境中需要保存配置数据,一般采用Unicode格式。在java中,默认是ASCII码格式文件。所以在读取Unicode格式文件的时候,要制定字符集。

譬如有一个Unicode格式的文件,命名为data.conf。里面保存一些名/值对(key/value pari),和properties文件内容是一样的,但是文件后缀名是.conf。现在想读取该文件。

 

        String filePath = getFilePath("data.conf");
        InputStreamReader isr = null;
        try {
            //Unicode file.
            isr = new InputStreamReader(new FileInputStream(filePath ), "UTF-16");
//          isr = new InputStreamReader(new FileInputStream(filePath ), "UNICODE");
            final Properties props = new Properties();
            props.load(isr);

           //handle properties here
        } catch (final FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (final IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                isr.close();
            } catch (final IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }
 

 

 

这样就可以正确地读出Unicode编码的文件内容了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值