簡體   English   中英

如何從raw文件夾中選擇一個txt文件,獲取路徑並讀取數據?

[英]How to choose a txt file from raw folder, get path and read data?

我有一個大約 40mbs 的大文本文件,我一直在尋找一種方法來讀取它的內容,我發現了這個:

File f = new File(String.valueOf(getResources().openRawResource(R.raw.test)));

try {
    FileInputStream inputStream = new FileInputStream(f);
    Scanner sc = new Scanner(inputStream, "UTF-8");
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

不幸的是我收到以下錯誤:

java.io.FileNotFoundException: android.content.res.AssetManager$AssetInputStream@ead83c (No such file or directory)

我試圖訪問的文件位於原始文件夾中,其名稱為test.txt

我哪里弄錯了?

Resources.openRawResource已經為您提供了InputStream

final InputStream inputStream = getResources().openRawResource(R.raw.test);
try {
    Scanner sc = new Scanner(inputStream, "UTF-8");
    // ...
} finally {
    inputStream.close();
}

原始資源打包在 APK 中。 使用您習慣的文件系統路徑無法訪問它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM