Monday, January 9, 2012

how can you read and write data from file in android

This article is very helpful for you .This function is used to write json data in file

set permission in manifest
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

public static void WriteSettings(Context context, String data, String file) throws IOException {
 FileOutputStream fos= null;
 OutputStreamWriter osw = null;
 fos= context.openFileOutput(file,Context.MODE_PRIVATE);
 osw = new OutputStreamWriter(fos);
This function is used to read file data.
public static String ReadSettings(Context context, String file) throws IOException {
 FileInputStream fis = null;
 InputStreamReader isr = null;
 String data = null;
 fis = context.openFileInput(file);
 isr = new InputStreamReader(fis);
 char[] inputBuffer = new char[fis.available()];;
 data = new String(inputBuffer);
 return data;

No comments:

Post a Comment