1 5 package org.relique.jdbc.csv; 6 7 import java.io.BufferedReader ; 8 import java.io.FileInputStream ; 9 import java.io.FileNotFoundException ; 10 import java.io.IOException ; 11 import java.io.InputStreamReader ; 12 import java.io.RandomAccessFile ; 13 import java.io.UnsupportedEncodingException ; 14 15 19 public class CsvRandomAccessFile extends RandomAccessFile { 20 21 private String fileName = null; 22 private String charset = null; 23 private long position = 0; 24 25 public CsvRandomAccessFile(String fileName, String charset) throws FileNotFoundException , UnsupportedEncodingException { 26 super(fileName,"rw"); 27 if(charset != null) 28 this.charset = charset; 29 this.fileName = fileName; 30 } 31 32 public String readCsvLine() throws IOException { 33 String retVal = null; 34 if(Utils.isUTF16(this.charset)) { 35 FileInputStream is = new FileInputStream (fileName); 36 BufferedReader reader = new BufferedReader (new InputStreamReader (is,charset)); 37 is.getChannel().position(this.position); 38 retVal = reader.readLine(); 39 if(retVal != null && retVal.equals("")) { 40 retVal = reader.readLine(); 41 } 42 int length = 0; 43 if(retVal != null) { 44 length = retVal.getBytes(this.charset).length; 45 } 46 this.position += length; 47 super.seek(this.position); 48 reader.close(); 49 is.close(); 50 } else { 51 retVal = super.readLine(); 52 } 53 return retVal; 54 } 55 56 public void seek(long pos) throws IOException { 57 super.seek(pos); 58 this.position = pos; 59 } 60 61 } 62 | Popular Tags |