Android read-write files and permissions settings

Source: Internet
Author: User

    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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.