public static Boolean saveinfo (
context Context, string userName, string userpass, int mode) {
try {
FileOutputStream Fos;
Switch (mode) {
Case 0:
FOS = Context.openfileoutput (
"Private.txt", context.mode_private);
Fos.write ((username+ "# #" +userpass). GetBytes ());
Fos.close ();
Break
Case 1:
FOS = Context.openfileoutput (
"Readable.txt", context.mode_world_readable);
Fos.write ((username+ "# #" +userpass). GetBytes ());
Fos.close ();
Break
Case 2:
FOS = Context.openfileoutput (
"Writeable.txt", context.mode_world_writeable);
Fos.write ((username+ "# #" +userpass). GetBytes ());
Fos.close ();
Break
Case 3:
FOS = Context.openfileoutput (
"Public.txt", context.mode_world_readable+context.mode_world_writeable);
Fos.write ((username+ "# #" +userpass). GetBytes ());
Fos.close ();
Break
Default
Break
}
return true;
} catch (Exception e) {
E.printstacktrace ();
return false;
}
}
Here is the program code to read the file and write the file:
Read file:
public void Readinfo (view view) {
File File = new file ("/data/data/com.aaron.login/files/public.txt");
FileInputStream fis;
try {
FIS = new FileInputStream (file);
BufferedReader br = new BufferedReader (new InputStreamReader (FIS));
String result = Br.readline ();
Toast.maketext (Mainactivity.this,
Result
Toast.length_long). Show ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
Toast.maketext (Mainactivity.this,
"Failed to read file",
Toast.length_long). Show ();
}
}
Write file:
public void Writeinfo (view view) {
File File = new file ("/data/data/com.aaron.login/files/public.txt");
FileOutputStream Fos;
try {
FOS = new FileOutputStream (file);
Fos.write ("hahaha". GetBytes ());
Fos.close ();
Toast.maketext (Mainactivity.this,
"Write file succeeded",
Toast.length_long). Show ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
Toast.maketext (Mainactivity.this,
"Write file Failed",
Toast.length_long). Show ();
}
}
Read and write files and permission settings for Android