1. The files are stored in the bucket of the mobile phone record.
Private Context context;
Public FileService (Context context ){
This. context = context;
}
/**
* Save the content. Note: allow other applications to read and write the file.
* @ Param filename file name
* @ Param content File content
* @ Throws Exception
*/
Public void saveRW (String filename, String content) throws Exception {
FileOutputStream outStream = context. openFileOutput (filename,
Context. MODE_WORLD_READABLE + Context. MODE_WORLD_WRITEABLE );
OutStream. write (content. getBytes ());
OutStream. close ();
}
/**
* Reading file content
* @ Param filename file name
* @ Return
* @ Throws Exception
*/
Public String readFile (String filename) throws Exception {
FileInputStream inStream = context. openFileInput (filename );
Byte [] buffer = new byte [1024];
Int len = 0;
ByteArrayOutputStream outStream = new ByteArrayOutputStream ();
While (len = inStream. read (buffer ))! =-1 ){
OutStream. write (buffer, 0, len );
}
Byte [] data = outStream. toByteArray (); // obtain the binary data of the object.
OutStream. close ();
InStream. close ();
Return new String (data );
}
File Operation Mode: Context. MODE_PRIVATE indicates that the file is private and can only be used by the application to overwrite the source file.
The Context. APPEND mode is private. Append content to the object if the object exists.
Context. MODE_WORLD_READABLE: indicates that the current file can be read by other applications.
Context. MODE_WORLD_WRITEABLE: indicates that the current file can be written by other applications.
If you want the file to be read and written by other applications, you can pass in:
OpenFileOutput ("zhgang.txt", Context. MODE_WORLD_READABLE +
Context. MODE_WORLD_WRITEABLE );
Androidhas a set of security models. When the application program (.apk) is installed, the system will assign a userid to it. When the application wants to access other resources, such as files
Userid match is required. By default, any application that creates files, sharedprefernces, and databases should be private (in/data/<package name>/files ),
Other programs cannot be accessed, unless Context. MODE_WORLD_READABLE and Context. MODE_WORLD_WRITEABLE are specified during creation. Only in this way can other programs be accessed correctly.
2> files are stored in SDCARD
Android2.2/mnt/sdcard
& Lt; 2.2/sdcard
Add permissions to SDCARD
<! -- Create and delete file permissions in SDCard -->
<Uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/>
<! -- Write data to SDCard -->
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
/** Www.2cto.com
* Save content as a private file
* @ Param filename file name
* @ Param content File content
* @ Throws Exception
*/
Public void saveToSDCard (String filename, String content) throws Exception {
File file = new File (Environment. getExternalStorageDirectory (), filename );
FileOutputStream outStream = new FileOutputStream (file );
OutStream. write (content. getBytes ());
OutStream. close ();
}
// Determine whether the sdcard exists on the mobile phone and can be read/written.
If (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED )){
FileService. saveToSDCard (filename, content );
Toast. makeText (MainActivity. this, R. string. success, 1). show ();
} Else {
Toast. makeText (MainActivity. this, R. string. sdcarderror, 1). show ();
}
Author: get123