Android : File Handling – Read/Write

To write to a text file in Android

FileOutputStream fOut = null;

OutputStreamWriter osw = null;

try{

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

osw = new OutputStreamWriter(fOut);

osw.write(“TEXT”);

osw.close();

fOut.close();

}catch(Exception e){

e.printStackTrace(System.err);

}

To read from the file

FileInputStream fIn = null;

InputStreamReader isr = null;

try{

char[] inputBuffer = new char[1024];

String data = null;

fIn = openFileInput(“public.dat”);

isr = new InputStreamReader(fIn);

isr.read(inputBuffer);

data = new String(inputBuffer);

isr.close();

fIn.close();

}catch(IOException e){

e.printStackTrace(System.err);

}

About these ads

Tags: , ,

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

  1. Asma Says:

    Hi,
    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:

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

  6. nireesha Says:

    Hi,

    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.
    OK

    Muhammad Mubashir.

  10. Pallavi Says:

    Thank you so much! this code works!!! :D
    B-)

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: