java - CSVReader 和 InputStream

标签 java android

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

}

最佳答案

你可以构建一个 InputStreamReader来自那个 InputStream

new InputStreamReader(myInputStream, encoding)

myInputStream 是您的 InputStreamencoding 是定义数据源使用的编码的 String

您可以这样调用您的 CSVReader:

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

关于java - CSVReader 和 InputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9663410/

相关文章:

java - 如何设置JtabbedPane选项卡中Jtable的大小以及如何设置JTabbedPane中选项卡的布局?

java - 构造函数引用和调用构造函数的 lambda 表达式有什么区别?

java - 我收到 NoSuchElement 异常错误

java - 在 fragment 之间传递数据

android - 使用带有 float editText 的 setErrorEnabled 时应用程序崩溃

java - ORMLite Android-从单个表中获取 17 列中的 1 列

javascript - 在服务器端解压LZString(java)

java - Spring RestTemplate 在尝试反序列化对象的嵌套列表时返回 null 对象

javascript - 如何在 Android 中将 Google Analytics 与 Phonegap 一起使用?

java - Android:在JAR中添加资源文件