Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSVReader and InputStream

Tags:

java

android

I have created CSVReader and I am trying to read csv file from assets for that reason I should use InputStream. But my code below does not have inputstream constructor. Could anyone tell me how i could add or change something in code, so I can use inputstream.

public class CSVReader {

    private BufferedReader br;

    private boolean hasNext = true;

    private char separator;

    private char quotechar;

    private int skipLines;

    private boolean linesSkiped;

    public int linesCount = 0;

    public static final char DEFAULT_SEPARATOR = '|';
    public static final char DEFAULT_QUOTE_CHARACTER = '"';
    public static final int DEFAULT_SKIP_LINES = 0;

    public CSVReader(Reader reader) {
        this(reader, DEFAULT_SEPARATOR, DEFAULT_QUOTE_CHARACTER,
            DEFAULT_SKIP_LINES);
    }

    public CSVReader(Reader reader, char separator, char quotechar, int line) {
        this.br = new BufferedReader(reader);
        this.separator = separator;
        this.quotechar = quotechar;
        this.skipLines = line;
    }
    public String[] readNext() throws IOException {

        String nextLine = getNextLine();
        return hasNext ? parseLine(nextLine) : null;
    }

    public String getNextLine() throws IOException {
        if (!this.linesSkiped) {
            for (int i = 0; i < skipLines; i++) {
                br.readLine();
            }
            this.linesSkiped = true;
        }
        String nextLine = br.readLine();
        if (nextLine == null) {
            hasNext = false;
        }
        return hasNext ? nextLine : null;
    }


    public List<String[]> readAll() throws IOException {

        List<String[]> allElements = new ArrayList<String[]>();
        while (hasNext) {
            String[] nextLineAsTokens = readNext();
            if (nextLineAsTokens != null)
                allElements.add(nextLineAsTokens);
        }
        return allElements;

    }

    private String[] parseLine(String nextLine) throws IOException {

        if (nextLine == null) {
            return null;
        }

        List<String> tokensOnThisLine = new ArrayList<String>();
        StringBuffer sb = new StringBuffer();
        boolean inQuotes = false;
        do {
            if (inQuotes) {
                // continuing a quoted section, reappend newline
                sb.append("\n");
                nextLine = getNextLine();
                linesCount++;
                if (nextLine == null)

                    break;
            }
            for (int i = 0; i < nextLine.length(); i++) {

                char c = nextLine.charAt(i);
                if (c == quotechar) {
                    if( inQuotes  
                        && nextLine.length() > (i+1)  
                        && nextLine.charAt(i+1) == quotechar ){ 
                        sb.append(nextLine.charAt(i+1));
                        i++;
                    }else{
                        inQuotes = !inQuotes;
                        if(i>2 
                                && nextLine.charAt(i-1) != this.separator 
                                && nextLine.length()>(i+1) &&
                                nextLine.charAt(i+1) != this.separator 
                        ){
                            sb.append(c);
                        }
                    }
                } else if (c == separator && !inQuotes) {
                    tokensOnThisLine.add(sb.toString());
                    sb = new StringBuffer(); 
                } else {
                    sb.append(c);
                }
            }
        } while (inQuotes);
        tokensOnThisLine.add(sb.toString());
        return (String[]) tokensOnThisLine.toArray(new String[0]);

    }

    public void close() throws IOException{
        br.close();
    }

}
like image 903
fish40 Avatar asked Mar 12 '12 07:03

fish40


People also ask

What is CSVReader in Java?

CSVReader – This class provides the operations to read the CSV file as a list of String array. CSVWriter – This class allows us to write the data to a CSV file. CsvToBean – This class will be used when you want to populate your java beans from a CSV file content.

What is Opencsv?

Opencsv is an easy-to-use CSV (comma-separated values) parser library for Java. It was developed because all the CSV parsers at the time didn't have commercial-friendly licenses.

How can I read a particular column of a particular row of a CSV file in Java?

-> You have one String i.e. row from file then you can simply use split(“,”) method to split row and separate each field. -> Now split() function will return an array which will be column wise data. Now you can access any column using its index.


1 Answers

You can construct an InputStreamReader from that InputStream

new InputStreamReader(myInputStream, encoding)

Where myInputStream is your InputStream and encoding is a String that defines the encoding used by your datasource.

You can call your CSVReader like this:

new CSVReader(new InputStreamReader(myInputStream, encoding));
like image 131
oers Avatar answered Sep 29 '22 12:09

oers