CSVReader and InputStream

9

我创建了CSVReader并尝试从资产中读取csv文件,因此我应该使用InputStream。但是我的下面的代码没有inputstream构造函数。有人能告诉我如何在代码中添加或更改一些内容,以便我可以使用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();
    }

}
1个回答

23
你可以使用该InputStream构造一个InputStreamReader
new InputStreamReader(myInputStream, encoding)

当您的数据源使用的编码方式已知时,可以通过以下方式调用CSVReader:

myInputStream是您的InputStreamencoding是定义数据源所使用编码的String
new CSVReader(new InputStreamReader(myInputStream, encoding));

2
你应该使用适当的双参数构造函数来指定读取时要使用的编码。 - Joachim Sauer
我不明白如何做这个。你能更具体地告诉我吗? - fish40
@fish40,有什么不清楚的吗?InpuStreamReader创建了一个Reader,可以在CSVReader构造函数中使用。 - oers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接