129

テキストファイルからテキストを読みたい。以下のコードでは、例外が発生します (つまり、catchブロックに移動します)。アプリケーションフォルダにテキストファイルを置きます。このテキスト ファイル (mani.txt) を正しく読み取るには、どこに配置すればよいですか?

    try
    {
        InputStream instream = openFileInput("E:\\test\\src\\com\\test\\mani.txt"); 
        if (instream != null)
        {
            InputStreamReader inputreader = new InputStreamReader(instream); 
            BufferedReader buffreader = new BufferedReader(inputreader); 
            String line,line1 = "";
            try
            {
                while ((line = buffreader.readLine()) != null)
                    line1+=line;
            }catch (Exception e) 
            {
                e.printStackTrace();
            }
         }
    }
    catch (Exception e) 
    {
        String error="";
        error=e.getMessage();
    }
4

7 に答える 7

262

これを試して :

テキストファイルはSDカードにあると思います

    //Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
    br.close();
}
catch (IOException e) {
    //You'll need to add proper error handling here
}

//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);

//Set the text
tv.setText(text.toString());

次のリンクも役立ちます。

AndroidでSDカードからテキストファイルを読み取るにはどうすればよいですか?

Androidでテキストファイルを読むには?

Android 読み取りテキスト生リソース ファイル

于 2012-09-14T09:38:31.600 に答える
31

SDカードからファイルを読みたい場合。次に、次のコードが役立つ場合があります。

 StringBuilder text = new StringBuilder();
    try {
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"testFile.txt");

        BufferedReader br = new BufferedReader(new FileReader(file));  
        String line;   
        while ((line = br.readLine()) != null) {
                    text.append(line);
                    Log.i("Test", "text : "+text+" : end");
                    text.append('\n');
                    } }
    catch (IOException e) {
        e.printStackTrace();                    

    }
    finally{
            br.close();
    }       
    TextView tv = (TextView)findViewById(R.id.amount);  

    tv.setText(text.toString()); ////Set the text to text view.
  }

    }

アセットフォルダーからファイルを読みたい場合は、

AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");

res/rawまたは、ファイルがインデックス化され、R ファイルの ID でアクセスできるフォルダーからこのファイルを読み取る場合:

InputStream is = getResources().openRawResource(R.raw.test);     

res/raw フォルダーからテキスト ファイルを読み取る良い例

于 2012-09-14T09:48:53.343 に答える
7

テキストファイルをアセットフォルダに入れます...&そのフォルダからファイルを読み取ります...

以下の参照リンクを参照してください...

http://www.technotalkative.com/android-read-file-from-assets/

http://sree.cc/google/reading-text-file-from-assets-folder-in-android

簡単なテキストファイルを読む

それが役立つことを願っています...

于 2012-09-14T09:42:31.737 に答える
3

まず、テキスト ファイルを raw フォルダーに保存します。

private void loadWords() throws IOException {
    Log.d(TAG, "Loading words...");
    final Resources resources = mHelperContext.getResources();
    InputStream inputStream = resources.openRawResource(R.raw.definitions);
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

    try {
        String line;
        while ((line = reader.readLine()) != null) {
            String[] strings = TextUtils.split(line, "-");
            if (strings.length < 2)
                continue;
            long id = addWord(strings[0].trim(), strings[1].trim());
            if (id < 0) {
                Log.e(TAG, "unable to add word: " + strings[0].trim());
            }
        }
    } finally {
        reader.close();
    }
    Log.d(TAG, "DONE loading words.");
}
于 2012-09-14T09:49:25.440 に答える
1

小さなテキスト ファイルの最短形式 (Kotlin):

val reader = FileReader(path)
val txt = reader.readText()
reader.close()
于 2020-09-12T17:07:22.307 に答える