java读取utf8类型的文件

本文介绍了在Java中读取UTF-8格式的文件时如何避免中文乱码的情况。通过对比三种不同的读取方式,强调了在创建InputStreamReader时指定字符集的重要性,特别是使用`charsetName = "UTF-8"`来确保正确解析包含中文的文件内容。
摘要由CSDN通过智能技术生成

  源文件为utf-8格式,且里面包含中文,如:

  in ('133','153','180','189') then '电信'

  in ('130','131','132','145','155','156','183','185','186') then '联通'

  1,不加转码的时候中文会乱码:

  Java代码

  1.public static void main(String[] args) throws Exception {

  2. File f = new File("http://www.cos120.com");

  3. InputStreamReader insReader = new InputStreamReader(new FileInputStream(f));

  4. BufferedReader bufReader = new BufferedReader(insReader);

  5. String line = new String();

  6. while ((line = bufReader.readLine()) != null) {

  7. System.out.println(line);

  8. }

  9. bufReader.close();

  10.}

  public static void main(String[] args) throws Exception {

  File f = new File("D

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值