编码问题

来源:互联网 发布:js端口探测 编辑:程序博客网 时间:2024/06/06 12:49

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
原创粉丝点击