0

I want to read from txt file and send to TextView. My method works on Java Project I read and I see System.out.print but the same method doesnt work in MainActivity. How can I fixed.Thanks

MainActivity

    public class MainActivity extends Activity {
    TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt=(TextView)findViewById(R.id.textView1);

        Parsing p =new Parsing();
        try {
            String gelen=p.readTxt();
            txt.setText(gelen);             
        } 
            catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }       
}

Parsing:

    public class Parsing {


    public String readTxt() throws FileNotFoundException
    {
         File file = new File("C:\\Users\\John\\Desktop\\try.txt");
            StringBuilder fileContents = new StringBuilder((int)file.length());
            Scanner scanner = new Scanner(file);
            String lineSeparator = System.getProperty("line.separator");

            try {
                while(scanner.hasNextLine()) {        
                    fileContents.append(scanner.nextLine() + lineSeparator);
                }
                return fileContents.toString();
            } finally {
                scanner.close();
            }
    }
}

I'm working it but I see just TextView.

Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76
  • 2
    you realy have that directory on your Android device? :-) C:\\Users\\John\\Desktop\\try.txt – nano_nano Dec 23 '13 at 11:59
  • Your android system cannot access "C:\\Users\\John\\Desktop\\try.txt".. You can put it under "raw" folder and access it – TheLostMind Dec 23 '13 at 12:01
  • That's a file on your PC. Your Android device/emulator is a different machine - it has no direct access to your PC file system. Add it to your Android project under raw or assets and access it from there. – NigelK Dec 23 '13 at 12:03
  • Although Android is like Java, we have some differences. The main difference between your Java code to Android code is the path. I suggest you to see this: http://stackoverflow.com/a/14377185/2652124 – Renan Bandeira Dec 23 '13 at 12:04

5 Answers5

3

you cannot specify the computer directory files to the android file path location to read lines in it.

  1. just put the file into your android project folder like assests and change the path and then try.
Nambi
  • 11,944
  • 3
  • 37
  • 49
1

How can I read a text file in Android?

I don't mean to be rude but try to search first. This question with similar problem was already asked.

I hope this link will help you.

Cheers

Community
  • 1
  • 1
Ratz
  • 81
  • 1
  • 9
  • Sam from me why don these guys rush into things i mean why don't they first thoroughly search. – Naseer Feb 21 '14 at 18:07
0

For file on sdcard ("sdcard\myfolder\1.txt") please use:

File file = new File(Environment.getExternalStorageDirectory(), "myfolder\1.txt");

Also dont forget:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Kulibin
  • 444
  • 4
  • 12
0

You must put the file in the asset folder of your project. Then to access it you can do something like

BufferedReader reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt")));

for more details read my answer here :

read file from assets

Community
  • 1
  • 1
HpTerm
  • 8,151
  • 12
  • 51
  • 67
0

First of all Android Application Project is different from Java Project.
You can not use File file = new File("C:\\Users\\John\\Desktop\\try.txt"); in android.

Place your text file in the /assets directory under the Android project. Use AssetManager class to access it.

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

If you are going to access file from memory card, Then use inputsteram is in your program. Also you need the following permission to read the text file

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Pradip
  • 3,189
  • 3
  • 22
  • 27
  • You are mixing things up. Adding the permission on the EXTERNAL_STORAGE has nothing to do with the asset folder. The asset folder is in your sandbox and is always accessible without setting any permission. Changing the permission on the external storage is only here to access data outside of the sandbox. – HpTerm Dec 23 '13 at 15:07