0
package com.example.popword;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.File;
import java.util.Scanner;

public class ReadText1 extends Activity {
    TextView textview;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview = (TextView) findViewById(R.id.textView);
        try (Scanner scanner = new Scanner(new File("C:\\Users\\UOU\\Desktop\\word.txt"))) {
            while (scanner.hasNext()) {
                textview.setText(scanner.nextLine());
            }
        } catch (Exception e) {
            System.err.println("Exception occurred!");
        }
    }
}

=================================================================================== 2019-11-13 20:07:44.224 1004-1004/com.example.popword E/cklee: com.example.popword.ReadText1.printStackTrace(ReadText1.java:29) com.example.popword.ReadText1.onCreate(ReadText1.java:22) android.app.Activity.performCreate(Activity.java:7136) android.app.Activity.performCreate(Activity.java:7127) android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) android.os.Handler.dispatchMessage(Handler.java:106) android.os.Looper.loop(Looper.java:193) android.app.ActivityThread.main(ActivityThread.java:6669) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Neil B
  • 2,096
  • 1
  • 12
  • 23
박종한
  • 1
  • 1
  • Please share your stacktrace (Exception) in the log so that we know exactly which type of exception is being thrown. Add e.printStackTrace(); into your catch block and post the console log output. – PauMAVA Nov 13 '19 at 11:02
  • 2
    `C:\\Users\\UOU\\Desktop\\word.txt` is not a file on your Android device/emulator. It's on your development machine. You cannot directly read that from an Android app. – Mike M. Nov 13 '19 at 11:07
  • don't ignore the exceoption, at least do `e.printStackTrace()` – Vladyslav Matviienko Nov 13 '19 at 13:26

3 Answers3

0

You need tu put your txt file inside Android project package. You can put it in :

  • Assets folder then :
AssetManager am = context.getAssets();
InputStream is = am.open("word.txt");
  • Res folder ( res/raw ) then :

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

Kévin Giacomino
  • 487
  • 4
  • 11
0

How can you read a file that is located on your computer? And there are lots of tutorials in the web explaining you how to read and write to files in android. And I would use BufferedReader instead of Scanner.

Intex32
  • 41
  • 1
  • 4
0

you can directly read file from storage or sdcard follow below step:

1.Add these two permission to your project manifest file

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2.Create below function

private void ReadTxtFile(File txtfilepath) {
    if (txtfilepath.exists()) {
        StringBuffer txt = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new FileReader(txtfilepath));
            String line;

            while ((line = br.readLine()) != null) {
                txt.append(line);
                txt.append('\n');
            }
            br.close();
        } catch (IOException e) {
            Log.e("Exception:",e.toString());
        }

        Log.e("File Text:",txt.toString());// "txt" variable contian your file text
    }
}
  1. Call function wherever you want and provide text file path as function parameter

     ReadTxtFile(new File("/sdcard/Download/sample.txt"));
    
Milan Tejani
  • 372
  • 5
  • 21