InputStream读取文件乱码

测试InputStream,读取一个中文文本,但发现在控制台上输出时,是乱码:

public class InputStream {
public static void main(String[] args) {
FileInputStream in = null;
int b;
long num = 0;
try {
//建立了一根管道,读取一个文件
in = new FileInputStream("L:\\Program Files\\test.txt");
while((b=in.read())!= -1) {
System.out.print((char)b);
num ++;
}
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件!");
System.exit(-1);
} catch (IOException e) {
System.out.println("文件读取错误");
System.exit(-1);
}
}
}

因为:

b=in.read())是读取一个字节,然后(char)b肯定会是乱码。
in = new FileInputStream("L:\\Program Files\\test.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/DryKillLogic/archive/2009/12/11/4984402.aspx

原帖:http://blog.csdn.net/DryKillLogic/archive/2009/12/11/4984402.aspx

转载于:https://www.cnblogs.com/jjtech/archive/2011/04/17/2019211.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值