malutan.mircea
malutan.mircea

Reputation: 143

read from text file in android

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!

Upvotes: 1

Views: 6403

Answers (4)

Adam Monos
Adam Monos

Reputation: 4307

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");
}

Upvotes: 0

HashtagMarkus
HashtagMarkus

Reputation: 1661

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.

Upvotes: 0

Daud Arfin
Daud Arfin

Reputation: 2499

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");

Upvotes: 0

dev_android
dev_android

Reputation: 8818

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
}

Upvotes: 1

Related Questions