Java字节流数据逐行读取(readLine),处理以Tab分隔符划分的数据

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

要处理一个平文本数据导入功能,因为原始数据有问题,部分字段有乱码和特殊字符,导致导出的数据在以文本形式查看时,里面的分隔符 /t 也就是Tab被弄错了,与前面的特殊字符组合成了一个乱码,导致最终数据使用 split("/t") 失败。这里使用字节流的方式,从最底层进行数据的抽取,以字节的 0x9; 作为字段分隔符,以 /r 和 /n 作为换行。完整代码如下:部分思路可以作为 BufferedReader + split 的替代品。


  1. [code=Java]package wis;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6. import java.nio.ByteBuffer;  
  7. import java.util.LinkedList;  
  8. import java.util.List;  
  9. /** 
  10.  * 逐行读取,以Tab分隔符划分的数据。<br> 
  11.  * 使用字节进行处理而不是字符串。<br> 
  12.  * 因为发现有些数据,由于特殊字符导致分隔符失效,所以必须在字节一级处理。 
  13.  *  
  14.  * @author JAVA世纪网(java2000.net, laozizhu.com) 
  15.  */  
  16. public class SplitFileReader {  
  17.   // 文件读取操作  
  18.   FileInputStream fis = null;  
  19.   // 文件读取的字节缓冲区  
  20.   byte[] bs = new byte[1024];  
  21.   // 当前数据在缓冲区的位置指针  
  22.   int pointer;  
  23.   // 保存字节,便于转换为字符串  
  24.   // 注意这里最大支持4k的字符串,请根据需要修改  
  25.   ByteBuffer bf = ByteBuffer.allocate(4096);  
  26.   /** 
  27.    * 指定文件名的构造函数 
  28.    *  
  29.    * @param filename 
  30.    * @throws FileNotFoundException 
  31.    */  
  32.   public SplitFileReader(String filename) throws FileNotFoundException {  
  33.     this(new File(filename));  
  34.   }  
  35.   /** 
  36.    * 指定文件的构造函数 
  37.    *  
  38.    * @param file 
  39.    * @throws FileNotFoundException 
  40.    */  
  41.   public SplitFileReader(File file) throws FileNotFoundException {  
  42.     if (!file.exists()) {  
  43.       throw new FileNotFoundException();  
  44.     }  
  45.     fis = new FileInputStream(file);  
  46.   }  
  47.   int len;  
  48.   byte b;  
  49.   /** 
  50.    * 读取一行数据 
  51.    *  
  52.    * @return 数据分割好的数组 
  53.    * @throws IOException 
  54.    */  
  55.   public List<String> readLine() throws IOException {  
  56.     List<String> list = new LinkedList<String>();  
  57.     // 读取数据  
  58.     while (pointer < len || (readMore() != -1)) {  
  59.       b = bs[pointer++];  
  60.       if (b == 9 || b == '/n') {  
  61.         list.add(new String(bf.array(), 0, bf.position(), "GBK"));  
  62.         bf.clear();  
  63.         if (b == '/n') {  
  64.           return list;  
  65.         }  
  66.       } else if (b != '/r') {  
  67.         bf.put(b);  
  68.       }  
  69.     }  
  70.     return list;  
  71.   }  
  72.   /** 
  73.    * 关闭 
  74.    */  
  75.   public void close() {  
  76.     bs = null;  
  77.     bf = null;  
  78.     try {  
  79.       fis.close();  
  80.     } catch (IOException e) {  
  81.       e.printStackTrace();  
  82.     }  
  83.   }  
  84.   private int readMore() throws IOException {  
  85.     len = fis.read(bs);  
  86.     pointer = 0;  
  87.     return len;  
  88.   }  
  89.   // 测试  
  90.   public static void main(String[] args) throws Exception {  
  91.     SplitFileReader reader = new SplitFileReader("c:/test.txt");  
  92.     List<String> list = reader.readLine();  
  93.     System.out.println(list);  
  94.   }  
  95. }  
  96. [/code]  
[code=Java]package wis;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.nio.ByteBuffer;import java.util.LinkedList;import java.util.List;/** * 逐行读取,以Tab分隔符划分的数据。<br> * 使用字节进行处理而不是字符串。<br> * 因为发现有些数据,由于特殊字符导致分隔符失效,所以必须在字节一级处理。 *  * @author JAVA世纪网(java2000.net, laozizhu.com) */public class SplitFileReader {  // 文件读取操作  FileInputStream fis = null;  // 文件读取的字节缓冲区  byte[] bs = new byte[1024];  // 当前数据在缓冲区的位置指针  int pointer;  // 保存字节,便于转换为字符串  // 注意这里最大支持4k的字符串,请根据需要修改  ByteBuffer bf = ByteBuffer.allocate(4096);  /**   * 指定文件名的构造函数   *    * @param filename   * @throws FileNotFoundException   */  public SplitFileReader(String filename) throws FileNotFoundException {    this(new File(filename));  }  /**   * 指定文件的构造函数   *    * @param file   * @throws FileNotFoundException   */  public SplitFileReader(File file) throws FileNotFoundException {    if (!file.exists()) {      throw new FileNotFoundException();    }    fis = new FileInputStream(file);  }  int len;  byte b;  /**   * 读取一行数据   *    * @return 数据分割好的数组   * @throws IOException   */  public List<String> readLine() throws IOException {    List<String> list = new LinkedList<String>();    // 读取数据    while (pointer < len || (readMore() != -1)) {      b = bs[pointer++];      if (b == 9 || b == '/n') {        list.add(new String(bf.array(), 0, bf.position(), "GBK"));        bf.clear();        if (b == '/n') {          return list;        }      } else if (b != '/r') {        bf.put(b);      }    }    return list;  }  /**   * 关闭   */  public void close() {    bs = null;    bf = null;    try {      fis.close();    } catch (IOException e) {      e.printStackTrace();    }  }  private int readMore() throws IOException {    len = fis.read(bs);    pointer = 0;    return len;  }  // 测试  public static void main(String[] args) throws Exception {    SplitFileReader reader = new SplitFileReader("c:/test.txt");    List<String> list = reader.readLine();    System.out.println(list);  }}[/code]
 

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

版权声明:本文为jgfyyfd原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。