java读取UTF-8文本文件第一个字符多出一个问号解决方法

1.创建工具类

[java]  view plain  copy
  1. import java.io.*;  
  2.   
  3. public class UnicodeReader extends Reader {  
  4.   PushbackInputStream internalIn;  
  5.   InputStreamReader   internalIn2 = null;  
  6.   String              defaultEnc;  
  7.   
  8.   private static final int BOM_SIZE = 4;  
  9.   
  10.     
  11.   UnicodeReader(InputStream in, String defaultEnc) {  
  12.      internalIn = new PushbackInputStream(in, BOM_SIZE);  
  13.      this.defaultEnc = defaultEnc;  
  14.   }  
  15.   
  16.   public String getDefaultEncoding() {  
  17.      return defaultEnc;  
  18.   }  
  19.   
  20.     
  21.   public String getEncoding() {  
  22.      if (internalIn2 == nullreturn null;  
  23.      return internalIn2.getEncoding();  
  24.   }  
  25.   
  26.     
  27.   protected void init() throws IOException {  
  28.      if (internalIn2 != nullreturn;  
  29.   
  30.      String encoding;  
  31.      byte bom[] = new byte[BOM_SIZE];  
  32.      int n, unread;  
  33.      n = internalIn.read(bom, 0, bom.length);  
  34.   
  35.      if ( (bom[0] == (byte)0x00) && (bom[1] == (byte)0x00) &&  
  36.                  (bom[2] == (byte)0xFE) && (bom[3] == (byte)0xFF) ) {  
  37.         encoding = "UTF-32BE";  
  38.         unread = n - 4;  
  39.      } else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) &&  
  40.                  (bom[2] == (byte)0x00) && (bom[3] == (byte)0x00) ) {  
  41.         encoding = "UTF-32LE";  
  42.         unread = n - 4;  
  43.      } else if (  (bom[0] == (byte)0xEF) && (bom[1] == (byte)0xBB) &&  
  44.            (bom[2] == (byte)0xBF) ) {  
  45.         encoding = "UTF-8";  
  46.         unread = n - 3;  
  47.      } else if ( (bom[0] == (byte)0xFE) && (bom[1] == (byte)0xFF) ) {  
  48.         encoding = "UTF-16BE";  
  49.         unread = n - 2;  
  50.      } else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) ) {  
  51.         encoding = "UTF-16LE";  
  52.         unread = n - 2;  
  53.      } else {  
  54.         // Unicode BOM mark not found, unread all bytes  
  55.         encoding = defaultEnc;  
  56.         unread = n;  
  57.      }      
  58.      //System.out.println("read=" + n + ", unread=" + unread);  
  59.   
  60.      if (unread > 0) internalIn.unread(bom, (n - unread), unread);  
  61.   
  62.      // Use given encoding  
  63.      if (encoding == null) {  
  64.         internalIn2 = new InputStreamReader(internalIn);  
  65.      } else {  
  66.         internalIn2 = new InputStreamReader(internalIn, encoding);  
  67.      }  
  68.   }  
  69.   
  70.   public void close() throws IOException {  
  71.      init();  
  72.      internalIn2.close();  
  73.   }  
  74.   
  75.   public int read(char[] cbuf, int off, int len) throws IOException {  
  76.      init();  
  77.      return internalIn2.read(cbuf, off, len);  
  78.   }  
  79.   
  80. }  

2.使用工具类读取文件

[java]  view plain  copy
  1. BufferedReader br = new BufferedReader(  
  2.      new UnicodeReader(  
  3.      new FileInputStream(sqlFile),   
  4.      Charset.defaultCharset().name()));   



3.出现有问号的编写

  1. File  new File("./utf.txt");  
  2.         FileInputStream in new FileInputStream(f);  
  3.         // 指定读取文件时以UTF-8的格式读取  
  4.         BufferedReader br new BufferedReader(new InputStreamReader(in, "UTF-8"));  
  5.           
  6.         String line br.readLine();  
  7.         while(line != null 
  8.          
  9.             System.out.println(line);  
  10.             line br.readLine();  
  11.         }  

只需编写工具类,将 new  InputStreamReader(in,  "UTF-8" )替换成

new UnicodeReader(new FileInputStream(sqlFile),Charset.defaultCharset().name())就可以解决该问题。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值