2012-03-12 5 views
6

CSVReader를 만들었으므로 자산에서 CSV 파일을 읽으려고합니다. 그런 이유로 InputStream을 사용해야합니다. 하지만 아래 코드는 inputstream 생성자가 없습니다. 아무도 내가 어떻게 코드를 추가하거나 변경할 수 있는지 알려 줄 수 없으므로 inputstream을 사용할 수 있습니다.CSVReader 및 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(); 
    } 

} 

답변

15

당신은 myInputStreamInputStreamencoding입니다의 InputStream

new InputStreamReader(myInputStream, encoding) 

에서 InputStreamReader을 구성 할 수 있습니다하면 데이터 소스에서 사용하는 인코딩을 정의하는 String이다.

이처럼 CSVReader를 호출 할 수

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

을 당신 * 정말해야 * 적절한 [2 개의 인수를 생성자 (https://developer.android.com를 사용하여 읽기에 사용하는 인코딩을 지정 /reference/java/io/InputStreamReader.html#InputStreamReader(java.io.InputStream,%20java.lang.String)). –

+0

나는 이것을 어떻게 이해하지 못합니다. 좀 더 구체적으로 말씀해 주시겠습니까? – fish40

+0

@ JoachimSauer 당신 말이 맞아요, 대답을 – oers