Write to and read from a file

Quickly share your Android Code Snippets here...

Write to and read from a file

Postby Marchu » Thu Oct 16, 2008 4:20 pm

If you want to save youre settings or other data, here is a example to write to and read from a file.
The file is private for the application, so isn't accessible for other apps or the user.

To write some string data to the device:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. // Save settings
  3.  
  4.      public void WriteSettings(Context context, String data){
  5.  
  6.          FileOutputStream fOut = null;
  7.  
  8.          OutputStreamWriter osw = null;
  9.  
  10.          
  11.  
  12.          try{
  13.  
  14.           fOut = openFileOutput("settings.dat",MODE_PRIVATE);      
  15.  
  16.           osw = new OutputStreamWriter(fOut);
  17.  
  18.           osw.write(data);
  19.  
  20.           osw.flush();
  21.  
  22.           Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
  23.  
  24.           }
  25.  
  26.           catch (Exception e) {      
  27.  
  28.           e.printStackTrace();
  29.  
  30.           Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
  31.  
  32.           }
  33.  
  34.           finally {
  35.  
  36.              try {
  37.  
  38.                     osw.close();
  39.  
  40.                     fOut.close();
  41.  
  42.                     } catch (IOException e) {
  43.  
  44.                     e.printStackTrace();
  45.  
  46.                     }
  47.  
  48.           }
  49.  
  50.      }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


to read the same settings:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. // Read settings
  3.  
  4.      public String ReadSettings(Context context){
  5.  
  6.          FileInputStream fIn = null;
  7.  
  8.          InputStreamReader isr = null;
  9.  
  10.          
  11.  
  12.          char[] inputBuffer = new char[255];
  13.  
  14.          String data = null;
  15.  
  16.          
  17.  
  18.          try{
  19.  
  20.           fIn = openFileInput("settings.dat");      
  21.  
  22.           isr = new InputStreamReader(fIn);
  23.  
  24.           isr.read(inputBuffer);
  25.  
  26.           data = new String(inputBuffer);
  27.  
  28.           Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
  29.  
  30.           }
  31.  
  32.           catch (Exception e) {      
  33.  
  34.           e.printStackTrace();
  35.  
  36.           Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
  37.  
  38.           }
  39.  
  40.           finally {
  41.  
  42.              try {
  43.  
  44.                     isr.close();
  45.  
  46.                     fIn.close();
  47.  
  48.                     } catch (IOException e) {
  49.  
  50.                     e.printStackTrace();
  51.  
  52.                     }
  53.  
  54.           }
  55.  
  56.                 return data;
  57.  
  58.      }
  59.  
  60.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


How to use the write method:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. WriteSettings(this,"setting0, setting1, setting2");
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


How to read the settings (put in a array for easier use):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String data[] = ReadSettings(this).split(",");
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Cheers
Marchu
Junior Developer
Junior Developer
 
Posts: 22
Joined: Sat Sep 27, 2008 4:11 pm
Location: Netherlands

Top

Postby pwhisler » Sun Dec 14, 2008 9:03 am

thank you, this is very helpful, but where does the settings.dat file get saved? how can I view it?
pwhisler
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Nov 08, 2008 8:32 pm

Postby haidong » Sun Dec 14, 2008 3:23 pm

pwhisler wrote:thank you, this is very helpful, but where does the settings.dat file get saved? how can I view it?


/data/data/PACKAGE_NAME/files/settings.dat
haidong
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Dec 14, 2008 3:21 pm

Postby pwhisler » Mon Dec 15, 2008 8:46 am

I put the code above in my class that extends Activity, and it seems to work, I get the toast message that it read and wrote the file, but my data folder is empty... it doesn't have the second data folder or anything else in it...
pwhisler
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Nov 08, 2008 8:32 pm

Postby pwhisler » Mon Dec 15, 2008 10:21 am

pwhisler wrote:I put the code above in my class that extends Activity, and it seems to work, I get the toast message that it read and wrote the file, but my data folder is empty... it doesn't have the second data folder or anything else in it...


oh i see, this works for the emulator, but how about on the actual device? or does it not matter, as long as it works on the emulator it will work on the device?

because i was running this off the G1 and my data folder was empty...
pwhisler
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Nov 08, 2008 8:32 pm

Postby MrSnowflake » Mon Dec 15, 2008 1:22 pm

pwhisler wrote:oh i see, this works for the emulator, but how about on the actual device? or does it not matter, as long as it works on the emulator it will work on the device?
When it works on the emulator it is supposed to work on a real device, though there are a lot of case where this isn't true, so you should have one test every app on a real device (actually you should anyway).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby tnagarajan » Wed Sep 30, 2009 3:26 pm

Hello Experts,

Is there a way to write and read a file outside the Application path?

Regards,
Nagarajan T.
tnagarajan
Freshman
Freshman
 
Posts: 2
Joined: Wed Sep 30, 2009 10:25 am

Postby saamyandroid » Fri Oct 02, 2009 8:11 pm

Ya u can do :



package com.saamy.net;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AndroidUI extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
InputStream is = getAssets().open("lan.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String text = new String(buffer);
long x = 1000;

TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(text);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
saamyandroid
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Sep 09, 2009 8:17 pm
Location: INDIA

Postby coolalok » Fri Mar 19, 2010 12:16 pm

IS there any answer to tnagarajan' s question?
Android......
Here I come!!!
User avatar
coolalok
Developer
Developer
 
Posts: 29
Joined: Tue Sep 01, 2009 7:35 pm
Location: Pune,India

appending lines

Postby i_raqz » Sun Mar 21, 2010 12:30 pm

i used the code that you gave and it worked great...but could you please tell me how can I open the file in apend mode.
Thanks,
raqz
i_raqz
Freshman
Freshman
 
Posts: 8
Joined: Sun Mar 21, 2010 12:26 pm
Location: Pittsburgh

Re: Write to and read from a file

Postby sparlay_pk » Thu Jun 10, 2010 1:41 pm

Assalamu elaikum.

how can we write/read data to/from phone internal memory, but this will not be a private folder of an application. Simply I want to write file to /data/data/myFile.xml , Also want to read from file stored in /data/data/


Please help me.

Allah Hafiz
sparlay_pk
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Jun 10, 2010 1:31 pm

Re: Write to and read from a file

Postby Abadus » Sat Jun 19, 2010 3:58 am

sparlay_pk wrote:Assalamu elaikum.

how can we write/read data to/from phone internal memory, but this will not be a private folder of an application. Simply I want to write file to /data/data/myFile.xml , Also want to read from file stored in /data/data/


Please help me.

Allah Hafiz



^^ this.
Abadus
Freshman
Freshman
 
Posts: 4
Joined: Fri Jun 18, 2010 12:08 pm

Re: Write to and read from a file

Postby Woodard2589 » Wed Sep 01, 2010 3:16 am

where do you put the

String data[] = ReadSettings(this).split(",");

i keep getting errors, it doesnt like going to and from an array. and i assume i remove the data = null
Woodard2589
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Sep 01, 2010 3:15 am

Re: Write to and read from a file

Postby seblog » Fri Jul 15, 2011 3:30 pm

by reading the settings.dat you put in this line of code:

char[] inputBuffer = new char[255];

that stops the reading after 255 chars...
what about if i need to read a file for which i don't know the fixed lenght?
how to modify the above to function properly?
thanks!
seblog
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jul 15, 2011 3:27 pm

Re: Write to and read from a file

Postby michalss » Sun Jul 31, 2011 8:52 pm

is there any chance to save it elswhere ? for example in /mnt/sdcard ??
michalss
Freshman
Freshman
 
Posts: 4
Joined: Sun Jul 31, 2011 5:31 pm

Top
Next

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 9 guests