15

Tôi đang cố gắng đọc trong một tệp văn bản của một loạt các từ mà tôi muốn sử dụng cho một trò chơi chữ mà tôi đang viết. Danh sách này được lưu trữ trong thư mục asset và là một tệp txt. Nhưng, bất cứ khi nào tôi cố gắng mở nó, nó ném một ngoại lệ.Đọc tệp từ thư mục nội dung ném FileNotFoundException

List<String>wordList = new ArrayList<String>(); 
    BufferedReader br = null; 
    try { 
     br = new BufferedReader(new InputStreamReader(getAssets().open("wordlist.txt"))); //throwing a FileNotFoundException? 
     String word; 
     while((word=br.readLine()) != null) 
     wordList.add(word); //break txt file into different words, add to wordList 
    } 
     catch(IOException e) { 
      e.printStackTrace(); 
     } 
     finally { 
      try { 
       br.close(); //stop reading 
      } 
      catch(IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
     String[]words = new String[wordList.size()]; 
     wordList.toArray(words); //make array of wordList 

     for(int i=0;i<words.length; i++) 
      Log.i("Brian", words[i]); //print out words in array 
} 

Dưới đây là các bản ghi lỗi, trong trường hợp đó là bất kỳ sự giúp đỡ:

02-22 20:49:47.646: WARN/System.err(2351): java.io.FileNotFoundException: wordlist.txt 
02-22 20:49:47.646: WARN/System.err(2351):  at android.content.res.AssetManager.openAsset(Native Method) 
02-22 20:49:47.746: WARN/System.err(2351):  at android.content.res.AssetManager.open(AssetManager.java:299) 
02-22 20:49:47.746: WARN/System.err(2351):  at android.content.res.AssetManager.open(AssetManager.java:273) 
02-22 20:49:47.756: WARN/System.err(2351):  at com.bic.anagram.GameActivity.onCreate(GameActivity.java:40) 
02-22 20:49:47.756: WARN/System.err(2351):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-22 20:49:47.756: WARN/System.err(2351):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521) 
02-22 20:49:47.756: WARN/System.err(2351):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574) 
02-22 20:49:47.766: WARN/System.err(2351):  at android.app.ActivityThread.access$2400(ActivityThread.java:121) 
02-22 20:49:47.766: WARN/System.err(2351):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1925) 
02-22 20:49:47.766: WARN/System.err(2351):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-22 20:49:47.776: WARN/System.err(2351):  at android.os.Looper.loop(Looper.java:136) 
02-22 20:49:47.776: WARN/System.err(2351):  at android.app.ActivityThread.main(ActivityThread.java:4425) 
02-22 20:49:47.776: WARN/System.err(2351):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-22 20:49:47.776: WARN/System.err(2351):  at java.lang.reflect.Method.invoke(Method.java:521) 
02-22 20:49:47.776: WARN/System.err(2351):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
02-22 20:49:47.776: WARN/System.err(2351):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
02-22 20:49:47.776: WARN/System.err(2351):  at dalvik.system.NativeStart.main(Native Method) 
02-22 20:49:47.776: WARN/dalvikvm(2351): threadid=3: thread exiting with uncaught exception (group=0x4001e280) 

Cảm ơn tất cả mọi người!

Trả lời

19

Kiểm tra xem tệp có được đóng gói đúng cách trong thư mục nội dung bên trong tệp .apk hay không. (Nó có thể được duyệt như một tập tin zip. Đổi tên nó nếu cần thiết để nhìn vào bên trong.)

+0

Điều thú vị đủ, các tập tin đã có khi tôi đã làm một xuất khẩu từ Eclipse, nhưng khi tôi kéo các tập tin ra khỏi điện thoại của tôi (từ data/app), toàn bộ thư mục tài sản là MIA. Làm cách nào để tôi tạo thư mục đó? –

+17

OK! Tôi đã phải kích chuột phải vào thư mục asset trong Eclipse sau đó chọn Build Path -> Use as Source Folder. Bây giờ Eclipse xây dựng nó khi nó gói gói ứng dụng. Đó là một gợi ý tốt. Cảm ơn! –

+0

Trên thực tế, tôi đã làm cho "đánh dấu tuyệt vời" đánh dấu chỉ một chút để nhanh chóng ... Nếu tôi đánh dấu "tài sản" như một thư mục Nguồn, nó không hoạt động - tập tin không được tìm thấy. Nếu tôi chỉ để cho nó được (tiêu chuẩn), nó làm việc tốt =) – Ted