Writing and reading files
Writing and reading files
Nov 14Been a busy weekend hacking on a project, a bit of Android development and a bit of web development.
Sadly I spent some time just working out how to read and write data to a flat file on Android. So I thought I’d share the code snippet I’m using
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);
osw.write(data);
osw.close();
fos.close();
}
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()];
isr.read(inputBuffer);
data = new String(inputBuffer);
isr.close();
fis.close();
return data;
}
