CSVReader和的InputStream [英] CSVReader and InputStream

查看:151
本文介绍了CSVReader和的InputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建CSVReader,我试图读取资产csv文件为这个原因,我应该使用的InputStream。但我的code以下不具备的InputStream的构造。谁能告诉我,我怎么可以添加或更改的东西在code,这样我就可以使用的InputStream。

 公共类CSVReader {

    私人的BufferedReader BR;

    私人布尔规则hasNext = TRUE;

    私人字符分隔符;

    私人焦quotechar;

    私人诠释skipLines;

    私人布尔linesSkiped;

    公众诠释linesCount = 0;

    公共静态最后的char DEFAULT_SEPARATOR ='|';
    公共静态最后的char DEFAULT_QUOTE_CHARACTER ='';
    公共静态最终诠释DEFAULT_SKIP_LINES = 0;

    公共CSVReader(阅读器阅读器){
        这个(读者,DEFAULT_SEPARATOR,DEFAULT_QUOTE_CHARACTER,
            DEFAULT_SKIP_LINES);
    }

    公共CSVReader(阅读器的读者,字符分隔,字符quotechar,诠释行){
        this.br =新的BufferedReader(读卡器);
        this.separator =分隔符;
        this.quotechar = quotechar;
        this.skipLines =行;
    }
    公众的String [] readNext()抛出IOException异常{

        串nextLine = getNextLine();
        返回规则hasNext? parseLine(nextLine):空;
    }

    公共字符串getNextLine()抛出IOException异常{
        如果(!this.linesSkiped){
            的for(int i = 0; I< skipLines;我++){
                br.readLine();
            }
            this.linesSkiped = TRUE;
        }
        串nextLine = br.readLine();
        如果(nextLine == NULL){
            规则hasNext = FALSE;
        }
        返回规则hasNext? nextLine:空;
    }


    公开名单<的String []> ReadAll方法()抛出IOException异常{

        名单<的String []> allElements =新的ArrayList<的String []>();
        而(规则hasNext){
            的String [] nextLineAsTokens = readNext();
            如果(nextLineAsTokens!= NULL)
                allElements.add(nextLineAsTokens);
        }
        返回allElements;

    }

    私有String [] parseLine(字符串nextLine)抛出IOException异常{

        如果(nextLine == NULL){
            返回null;
        }

        名单<字符串> tokensOnThisLine =新的ArrayList<字符串>();
        StringBuffer的SB =新的StringBuffer();
        布尔inQuotes = FALSE;
        做 {
            如果(inQuotes){
                //继续带引号的部分,reappend换​​行符
                sb.append(\ N);
                nextLine = getNextLine();
                linesCount ++;
                如果(nextLine == NULL)

                    打破;
            }
            的for(int i = 0; I< nextLine.length();我++){

                焦炭C = nextLine.charAt(我);
                如果(C == quotechar){
                    如果(inQuotes
                        &功放;&安培; nextLine.length()>第(i + 1)
                        &功放;&安培; nextLine.charAt第(i + 1)== quotechar){
                        sb.append(nextLine.charAt第(i + 1));
                        我++;
                    }其他{
                        !inQuotes = inQuotes;
                        如果(ⅰ→2
                                &功放;&安培; nextLine.charAt第(i-1)!= this.separator
                                &功放;&安培; nextLine.length()>第(i + 1)及;&安培;
                                nextLine.charAt第(i + 1)!= this.separator
                        ){
                            sb.append(C);
                        }
                    }
                }否则,如果(C ==分离器和放大器;&安培;!inQuotes){
                    tokensOnThisLine.add(sb.toString());
                    SB =新的StringBuffer();
                } 其他 {
                    sb.append(C);
                }
            }
        }而(inQuotes);
        tokensOnThisLine.add(sb.toString());
        返回(字符串[])tokensOnThisLine.toArray(新的String [0]);

    }

    公共无效的close()抛出IOException异常{
        br.close();
    }

}
 

解决方案

您可以构造一个<一个href="http://docs.oracle.com/javase/1.5.0/docs/api/java/io/InputStreamReader.html">InputStreamReader从InputStream的

 新的InputStreamReader(myInputStream,编码)
 

其中, myInputStream 的InputStream 编码是一个字符串定义所使用的数据源的编码。

您可以打电话给你CSVReader是这样的:

 新CSVReader(新的InputStreamReader(myInputStream,编码));
 

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();
    }

}

解决方案

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));

这篇关于CSVReader和的InputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆