I am writing to file in android and read from the same file using the below code:
//Write data on file
FileOutputStream fOut 开发者_如何学运维= null;
OutputStreamWriter osw = null;
try {
fOut = openFileOutput("gasettings.dat", MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
Toast.makeText(context, "Settings saved", Toast.LENGTH_SHORT)
.show();
}
catch (Exception e) {
e.printStackTrace();
}
and the code for reading from file is:
InputStreamReader isr = null;
fileInputStream fIn = null;
char[] inputBuffer = new char[255];
String data = null;
try {
fIn = openFileInput("gasettings.dat");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
}
catch (Exception e) {
e.printStackTrace();
}
as per now I am only able to save a string to this file. I like to write a DATE array to it and also want to read back the data as array. I know that the return type of read method will be changed, but I am not getting the idea of how to read and write the DATE array or any other array to the file.
Thanks
In that case, your better choice is using JSON. It will allow you to save an array in String format, read it back and convert it again into the original array.
Take a look of this example: http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
精彩评论