2012-03-12 12 views
6

मैंने CSVReader बनाया है और मैं उस कारण से सीएसवी फ़ाइल को संपत्ति से पढ़ने की कोशिश कर रहा हूं, मुझे इनपुटस्ट्रीम का उपयोग करना चाहिए। लेकिन नीचे मेरे कोड में इनपुटस्ट्रीम कन्स्ट्रक्टर नहीं है। क्या कोई मुझे बता सकता है कि मैं कोड में कुछ जोड़ या बदल सकता हूं, इसलिए मैं इनपुटस्ट्रीम का उपयोग कर सकता हूं।सीएसवीआरडर और इनपुटस्ट्रीम

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

आपको लगता है कि InputStream

new InputStreamReader(myInputStream, encoding) 

कहाँ myInputStream अपने InputStream और encoding है से एक InputStreamReader निर्माण कर सकते हैं एक String कि अपने डेटा स्रोत से प्रयुक्त एन्कोडिंग परिभाषित करता है।

आप इस तरह अपने CSVReader कॉल कर सकते हैं:

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

आप * वास्तव में होना चाहिए * उचित [दो तर्क निर्माता] (https://developer.android.com का उपयोग करके एन्कोडिंग पढ़ने के लिए उपयोग करने के लिए निर्दिष्ट /reference/java/io/InputStreamReader.html#InputStreamReader(java.io.InputStream,%20java.lang.String))। –

+0

मुझे समझ में नहीं आता कि यह कैसे करें। क्या आप मुझे अधिक विशेष रूप से बता सकते हैं। – fish40

+0

@JoachimSauer आप सही हैं, उत्तर – oers

संबंधित मुद्दे