Java CSVReader 和 InputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9663410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:48:35  来源:igfitidea点击:

CSVReader and InputStream

javaandroid

提问by fish40

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.

我已经创建了 CSVReader 并且我正在尝试从资产中读取 csv 文件,因此我应该使用 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();
    }

}

采纳答案by oers

You can construct an InputStreamReaderfrom that InputStream

您可以构造一个InputStreamReader的从InputStream中

new InputStreamReader(myInputStream, encoding)

Where myInputStreamis your InputStreamand encodingis a Stringthat defines the encoding used by your datasource.

哪里myInputStream是你的InputStream,并encoding是一个String定义你的数据源使用的编码。

You can call your CSVReader like this:

您可以像这样调用您的 CSVReader:

new CSVReader(new InputStreamReader(myInputStream, encoding));