编码问题

package io;
import java.io.*;
import java.util.Arrays;
public class TestFileInputStream {


 public static void main(String[] args) {
  
  
  int b=0;
  FileInputStream in = null;
 
  try{
  // FileInputStream in = new FileInputStream("F:/a.txt");
  in = new FileInputStream("F:/a.txt");
   
  }catch(FileNotFoundException e){
   
   System.out.println("找不到文件");
   System.exit(0);
  }
  
  try{
//   while( (b = in.read() ) !=-1){
//    System.out.print((char)b);
//    System.out.print(b);
//   }    //存为ASSIC码时,英文正常,出现中文乱码  ,文件存为unicode时,?255?254a97 0b98 0c99 017b98/47f102-45N78?253V86?186N78
//   
   
   byte[] buffer = new byte[20];
   int len = in.read(buffer);
   String content = new String(buffer,0,len);
   System.out.println(Arrays.toString(buffer));
   System.out.println(content+"len = "+len); //不会出现中文乱码,使用了读入时缓冲
//ASSIC:   [97, 98, 99, -50, -46, -54, -57, -42, -48, -71, -6, -56, -53, 13, 10, 0]
//     abc我是中国人
//unicode: [-1, -2, 97, 0, 98, 0, 99, 0, 17, 98, 47, 102, 45, 78, -3, 86, -70, 78, 0, 0]
//   ?a b c b/f-N齎篘len = 18
  }catch(IOException e){
   
   System.out.println("读取错误");//汉字输出为乱码
   System.exit(0);
  } 
 }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值