KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > relique > jdbc > csv > CsvRandomAccessFile


1 /*
2  *
3  * Created on Nov 23, 2004
4  */

5 package org.relique.jdbc.csv;
6
7 import java.io.BufferedReader JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.FileNotFoundException JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStreamReader JavaDoc;
12 import java.io.RandomAccessFile JavaDoc;
13 import java.io.UnsupportedEncodingException JavaDoc;
14
15 /**
16  *
17  * @author Zoran Milakovic
18  */

19 public class CsvRandomAccessFile extends RandomAccessFile JavaDoc {
20
21     private String JavaDoc fileName = null;
22     private String JavaDoc charset = null;
23     private long position = 0;
24     
25     public CsvRandomAccessFile(String JavaDoc fileName, String JavaDoc charset) throws FileNotFoundException JavaDoc, UnsupportedEncodingException JavaDoc {
26         super(fileName,"rw");
27         if(charset != null)
28             this.charset = charset;
29         this.fileName = fileName;
30     }
31     
32     public String JavaDoc readCsvLine() throws IOException JavaDoc {
33         String JavaDoc retVal = null;
34         if(Utils.isUTF16(this.charset)) {
35             FileInputStream JavaDoc is = new FileInputStream JavaDoc(fileName);
36             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(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 JavaDoc {
57         super.seek(pos);
58         this.position = pos;
59     }
60     
61 }
62
Popular Tags