CSVReader.java

package ac.essex.ooechs.imaging.commons.util; 
 
import java.util.Vector; 
import java.util.StringTokenizer; 
import java.io.File; 
import java.io.IOException; 
import java.io.BufferedReader; 
import java.io.FileReader; 
 
/** 
 * Very simple SEPARATED-VALUE database reader. Depending on the file extension, 
 * this will split up CSV (comma separated values), TSV (tab separated values) or 
 * SSV (space separated values) files. 
 * 
 * @author Olly Oechsle, University of Essex, Date: 19-Oct-2007 
 * @version 1.1 SSV Support 
 * @version 1.2 Supports "quotation" marks in CSV 
 */ 
public class CSVReader { 
 
    public static final char DEFAULT_SEPARATOR = ','; 
    public static final char LINE_COMMENT_START = '#'; 
 
    Vector<Vector<String>> lines; 
    int position = 0; 
 
    public static void main(String[] args) throws Exception { 
        File f = new File("/home/ooechs/Desktop/EE111_Marked/EE111_Marks.csv"); 
        CSVReader reader = new CSVReader(f); 
        while (reader.hasMoreLines()) { 
            reader.getLine(); 
            System.out.println(reader.getInt(0)); 
            System.out.println(reader.getString(1)); 
        } 
    } 
 
    public CSVReader(File file) throws IOException { 
 
        if (file.getName().toLowerCase().endsWith(".tsv")) { 
            load(file, '\t'); 
            return; 
        } 
 
        if (file.getName().toLowerCase().endsWith(".ssv")) { 
            load(file, ' '); 
            return; 
        } 
 
        load(file, DEFAULT_SEPARATOR); 
 
    } 
 
    public CSVReader(File file, char separator) throws IOException { 
        load(file, separator); 
    } 
 
    private void load(File file, char separator) throws IOException { 
        // read lines from the file 
        lines = new Vector<Vector<String>>(500); 
        BufferedReader reader = new BufferedReader(new FileReader(file)); 
        String line; 
        while ((line = reader.readLine()) != null) { 
            if (line.trim().charAt(0) == LINE_COMMENT_START) continue; 
            Vector<String> items = new Vector<String>(5); 
 
            char[] characters = line.toCharArray(); 
            StringBuffer buffer = new StringBuffer(); 
            boolean insideQuotes = false; 
            for (int i = 0; i < characters.length; i++) { 
                char character = characters[i]; 
                if (character == '"') { 
                    insideQuotes = !insideQuotes; 
                    continue; 
                } 
                if (!insideQuotes && character == separator) { 
                    items.add(buffer.toString().trim()); 
                    buffer = new StringBuffer(); 
                    continue; 
                } 
                buffer.append(character); 
            } 
 
            if (buffer.length() > 0) items.add(buffer.toString().trim()); 
 
            lines.add(items); 
        } 
    } 
 
    public boolean hasMoreLines() { 
        return position < lines.size(); 
    } 
 
    Vector<String> currentLine; 
 
    public Vector<String> getLine() { 
        position++; 
        currentLine = lines.elementAt(position - 1); 
        return currentLine; 
    } 
 
    public int getInt(int index) { 
        return Integer.parseInt(currentLine.elementAt(index)); 
    } 
 
    public double getDouble(int index) { 
        return Double.parseDouble(currentLine.elementAt(index)); 
    } 
 
    public float getFloat(int index) { 
        return Float.parseFloat(currentLine.elementAt(index)); 
    } 
 
    public String getString(int index) { 
        return currentLine.elementAt(index); 
    } 
 
}