Android : File Handling – Read/Write

To write to a text file in Android

FileOutputStream fOut = null;

OutputStreamWriter osw = null;


fOut = openFileOutput(“public.dat”, Context.MODE_PRIVATE);

osw = new OutputStreamWriter(fOut);




}catch(Exception e){



To read from the file

FileInputStream fIn = null;

InputStreamReader isr = null;


char[] inputBuffer = new char[1024];

String data = null;

fIn = openFileInput(“public.dat”);

isr = new InputStreamReader(fIn);;

data = new String(inputBuffer);



}catch(IOException e){




Tags: , ,

12 Responses to “Android : File Handling – Read/Write”

  1. Asma Says:

    After entering this code, there is no error (some tiny error but I solved). But it doesn’t showing anything. To execute this program what should I have to do?

    • narenst Says:

      The content in the file is now available in ‘data’ variable. you can use it any way you want. I have not printed it so you might not see anything.

      • Meet Says:

        this is still not working … no error but not showing anything… even if i m searching for the filename ,it will not showing any file of that name.

  2. Naresh Says:

    thank you..!
    it is very useful me..

  3. Joshua Says:

    But when I print the retrieved string to the console, I just get gibberish.

    Please help. This is driving me mad.

  4. Shreeranjni R Says:

    when i first create the program it works. but if i change the file name later, it is not working.. !! like if i change “public.dat” to “myfile.dat” after executing the prog once. y is it happening? if i change file name both while reading and writing, it should work !

  5. Ramash Says:

    Where can I find public.dat file. I’m running in android emulator

  6. nireesha Says:


    To read file public.dat where should I put in.. like for layout we place all layouts in res\layout.. is thr any specific location for these files…

  7. abdulrehman Says:

    thanx for uploading this code can you upload code for parsing xml in android and databases examples plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

  8. kashif Says:

    hey , where this file public.dat should be placed on the disk??
    I am just getting blank screen….

  9. muhammad mubashir Says:

    Basically to parse something its depends on your requirements.
    so Try if you stuck in any thing then share with me..

    and i wll try to parse any Element based xml then should send to you.

    Muhammad Mubashir.

  10. Pallavi Says:

    Thank you so much! this code works!!! 😀

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: