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!

Michal
  • 15,429
  • 10
  • 73
  • 104
malutan.mircea
  • 143
  • 1
  • 2
  • 12
  • 2
    Where are you storing your textfile? – VendettaDroid Aug 29 '12 at 06:56
  • 1
    Similar to the following: http://stackoverflow.com/questions/3344551/how-to-read-text-file-in-android and http://stackoverflow.com/questions/3316629/how-to-read-file-from-the-phone-memory-in-android – Joey Aug 29 '12 at 06:59
  • Is your text file stored in your application or in external storage? –  Aug 29 '12 at 07:08
  • Possible duplicate of [How can I read a text file in Android?](https://stackoverflow.com/questions/12421814/how-can-i-read-a-text-file-in-android) – mike47 Jul 17 '17 at 05:16

4 Answers4

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
}
dev_android
  • 8,698
  • 22
  • 91
  • 148
  • 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. – malutan.mircea Aug 29 '12 at 07:27
  • 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 – dev_android Aug 29 '12 at 07:33
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");
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
  • you use \ instead of / He would get an error if he used your answer. –  Aug 29 '12 at 07:13
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.

HashtagMarkus
  • 1,641
  • 11
  • 20
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");
}
Adam Monos
  • 4,287
  • 1
  • 23
  • 25