2013-03-21 73 views
2

我正嘗試從android資源文件夾讀取csv文件。我已經看過其他帖子,似乎我已經建立了與其他人一樣。我已嘗試this.getAssets()並將上下文傳遞給該方法並使用context.getAssests()以下是我用於讀取該文件的代碼。並將這些信息放入數據庫中。無法從資產中讀取csv文件

try{ 
     AssetManager mng = getApplicationContext().getAssets(); 
     InputStream is = mng.open("word_types.csv"); 
     CSVReader csv = new CSVReader(new InputStreamReader(is)); 

     String[] s; 
     while((s = csv.readNext()) != null){ 
      db.addWordType(Integer.parseInt(s[0]), s[1], Integer.parseInt(s[2])); 
     } 
     csv.close(); 
    }catch(IOException ioe){ 
     Log.e("File Read Error: ","" + ioe.getMessage()); 
    } 

    for(int i=1;i<=12 ;i++) 
      Log.i(i+" = ", ""+ db.getWordType(i)); 

以下是在logcat中打印出:

E/File Read Error: (25767): word_types.csv 
I/1 =  (25767): null 
I/2 =  (25767): null 
I/3 =  (25767): null 
I/4 =  (25767): null 
I/5 =  (25767): null 
I/6 =  (25767): null 
I/7 =  (25767): null 
I/8 =  (25767): null 
I/9 =  (25767): null 
I/10 = (25767): null 
I/11 = (25767): null 
I/12 = (25767): null 
+1

將'ioe'作爲第三個參數傳遞給你的'Log.e()'調用,所以你可以看到完整的堆棧跟蹤。 – CommonsWare 2013-03-21 18:39:21

+0

謝謝。不知道這一點。 – Zabador 2013-03-21 19:07:01

回答

4

我意識到我犯了一個愚蠢的錯誤。謝謝你CommonsWare告訴我打印這樣的堆棧跟蹤。我不知道如何將ioe添加爲第三個參數。在查看堆棧跟蹤之後,我意識到我拼寫錯誤的資產目錄。我有資格。

+1

堆棧痕跡是你的朋友! :-) – CommonsWare 2013-03-21 19:17:21