1

I am new in android development, and I'm trying to create a simple application which reads some data from a text file and displays it in a ListView. The problem is my reader doesn't find my file. I've debugged my application and that is the conclusion I've come up with. So, where does the text file have to placed in order for the reader to find it? Heres some code:

 try { FileInputStream fstream = new FileInputStream("movies.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; while ((strLine = br.readLine()) != null) { filme.add(strLine); Log.d(LOG_TAG,"movie name:" + strLine); } in.close(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } 

Thanks!

4

4 Answers 4

1

Put the file named movies.txt in res/raw, then use the following code

String displayText = ""; try { InputStream fileStream = getResources().openRawResource( R.raw.movies); int fileLen = fileStream.available(); // Read the entire resource into a local byte buffer. byte[] fileBuffer = new byte[fileLen]; fileStream.read(fileBuffer); fileStream.close(); displayText = new String(fileBuffer); } catch (IOException e) { // exception handling } 
Sign up to request clarification or add additional context in comments.

2 Comments

OK, I've created my raw folder in res, I've checked to see that i don't have any android.R imports, but now i get this error on the following line: InputStream fileStream = getResources().openRawResource(R.raw.movies); ERROR : raw cannot be resolved or is not a filed.
After adding it, the file should show in the eclipse package explorer. If necessary, clean the project once. It should automatically create R object. And therefore you should get it in R.raw, just like any image is available in R.drawable
0
FileInputStream fstream = new FileInputStream("movies.txt"); 

where is the path for movies.txt ?? You must need to give the path as sd card or internal storage wherever you have stored.

As if, it is in sd card

 FileInputStream fstream = new FileInputStream("/sdcard/movies.txt"); 

1 Comment

you use \ instead of / He would get an error if he used your answer.
0

Usually when you want to open a file you put it into the res folder of your project. When you want to open a text file, you can put it into the res/raw directory. Your Android eclipse plugin will generate a Resource class for you containing a handle to your textfile.

To access your file you can use this in your activity:

InputStream ins = getResources().openRawResource(R.raw.movies); 

where "movies" is the name of your file without the filetype.

Comments

0

If you store your files on the SD card, then you can get the root of the SD card with Environment.getExternalStorageDirectory().

Note, that you might not be able to access the SD card, if it is mounted to the computer for example.

You can check the state of the external storage like this:

boolean externalStorageAvailable = false; boolean externalStorageWriteable = false; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { externalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { externalStorageAvailable = true; externalStorageWriteable = false; } else { externalStorageAvailable = mExternalStorageWriteable = false; } if(externalStorageAvailable && externalStorageWriteable){ File sdRoot = Environment.getExternalStorageDirectory(); File myFile = new File(sdRoot, "path/to/my/file.txt"); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.