2013-11-14 97 views
6

我正在開發一個netbeans中的android應用程序。我正在嘗試使用opencsv讀取CSV文件。當我將文件放在資源文件夾中並嘗試從那裏讀取文件時,在構建無效的資源目錄時出現錯誤。我應該在哪裏存儲csv文件,以便每次啓動應用程序時都可以讀取它?讀取資源文件夾中的CSV文件android

回答

7

你應該把資產的文件夾中的CSV文件..

InputStreamReader is = new InputStreamReader(getAssets() 
         .open("filename.csv")); 

       BufferedReader reader = new BufferedReader(is); 
       reader.readLine(); 
       String line; 
       while ((line = reader.readLine()) != null) { 

        } 
1

您可以使用此代碼

try { 
       InputStream csvStream = assetManager.open(CSV_PATH); 
       InputStreamReader csvStreamReader = new  InputStreamReader(csvStream); 
       CSVReader csvReader = new CSVReader(csvStreamReader); 
       String[] line; 

       // throw away the header 
       csvReader.readNext(); 

       while ((line = csvReader.readNext()) != null) { 
        questionList.add(line); 
       } 
       } catch (IOException e) { 
       e.printStackTrace(); 
       } 

你可以從 http://sourceforge.net/projects/opencsv/files/latest/download

和進口項目中的下載csvreader文件

7

一些建議;

  • 創建一個對象,用於將一行數據保存到csv中。 (Ex: YourSimpleObject。它提供給你輕鬆管理數據。)
  • 逐行讀取文件並分配給對象。將該對象添加到列表中。 (例如:ArrayList<YourSimpleObject >

代碼:

private void readAndInsert() throws UnsupportedEncodingException { 


ArrayList<YourSimpleObject > objList= new ArrayList<YourSimpleObject >(); 
AssetManager assetManager = getAssets(); 
InputStream is = null; 

      try { 
       is = assetManager.open("questions/question_bank.csv"); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      BufferedReader reader = null; 
      reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); 

      String line = ""; 
      StringTokenizer st = null; 
      try { 

       while ((line = reader.readLine()) != null) { 
        st = new StringTokenizer(line, ","); 
        YourSimpleObject obj= new YourSimpleObject(); 
            //your attributes 
        obj.setX(st.nextToken()); 
        obj.setY(st.nextToken()); 
        obj.setZ(st.nextToken()); 
        obj.setW(st.nextToken()); 

        objList.add(sQuestion); 

       } 
      } catch (IOException e) { 

       e.printStackTrace(); 
      } 



} 
0

使用opencsv:

InputStream is = context.getAssets().open(path); 
InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8")); 
List<String[]> csv = new CSVReader(reader).readAll(); 
1

作爲替代方案,看看uniVocityParsers。它提供了大量解析分隔文件的方法。下面的示例將resv/raw文件夾中的Csv文件(參見下圖)加載到InputStream對象中,並以colunar方式(key = Column & value = ColumnValues)讀取它。

calendario_bolsa.csv

//Gets your csv file from res/raw dir and load into a InputStream. 
InputStream csvInputStream = getResources().openRawResource(R.raw.calendario_bolsa); 

//Instantiate a new ColumnProcessor 
ColumnProcessor columnProcessor = new ColumnProcessor(); 

//Define a class that hold the file configuration 
CsvParserSettings parserSettings = new CsvParserSettings(); 
parserSettings.getFormat().setLineSeparator("\n"); 
parserSettings.setHeaderExtractionEnabled(true); 
parserSettings.setProcessor(columnProcessor); 

//Creates a new CsvParser, passing the settings into its construtor: 
CsvParser csvParser = new CsvParser(parserSettings); 

//Calls parse method, instantiating an InputStreamReader, passing to its constructor the InputStream object 
csvParser.parse(new InputStreamReader(csvInputStream)); 

//Gets the csv data as a Map of Column/column values. 
Map<String, List<String>> columnarCsv = columnProcessor.getColumnValuesAsMapOfNames(); 

要添加univocityParsers到你的Android項目:

compile group: 'com.univocity', name: 'univocity-parsers', version: '2.3.0' 
相關問題