Android data storage files
File Operations in Android are the same as those in Java.
In Android systems, storage systems are classified into internal storage and external storage.
Internal Storage space
RAM memory: The Running memory, which is equivalent to the computer memory.
ROM memory: storage memory, equivalent to the hard disk of a computer
Write Data in ROM memory without permission
In Android, components cannot convert EditText to EditText.
Remember the logon username and password in the case:
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical"
>
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/name"
Android: hint = "Enter the User name:"/>
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: id = "@ + id/password"
Android: password = "true"
Android: hint = "enter the password:"/>
Android: layout_width = "fill_parent"
Android: layout_height = "match_parent"
>
Android: id = "@ + id/login"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: text = "login"
Android: onClick = "login"/>
Android: id = "@ + id/check"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignBaseline = "@ + id/login"
Android: layout_alignBottom = "@ + id/login"
Android: layout_centerVertical = "true"
Android: text = "Remember User Name"/>
Package com. test. storage;
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. CheckBox;
Import android. widget. EditText;
Import android. widget. Toast;
Public class MainActivity extends Activity {
Private EditText et_name = null;
Private EditText et_password = null;
Private String name = "";
Private String password = "";
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
// The initialization of global variables, preferably in the onCreate Method
// Do not obtain the value of the component in advance, which may cause the value to change or be empty next time.
/* Et_name = (EditText) this. findViewById (R. id. name );
Et_password = (EditText) this. findViewById (R. id. password );
Name = et_name.getText (). toString (). trim ();
Password = et_password.getText (). toString (). trim ();*/
Et_name = (EditText) this. findViewById (R. id. name );
Et_password = (EditText) this. findViewById (R. id. password );
Try {
ReadInfo ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Public void login (View v) throws IOException {
Name = et_name.getText (). toString (). trim ();
Password = et_password.getText (). toString (). trim ();
CheckBox cb = (CheckBox) this. findViewById (R. id. check );
// Determine whether the checkbox is selected
If (cb. isChecked ()){
// Path of the internal bucket data/package name in RAM
// It is best to obtain the Context Environment Information through Context, getFilesDir ():/data/current package name. apirwinfrom/files/file name
// File file = new File (getFilesDir (), "userinfo.txt ");
// This method is not good. If the package name changes, an exception is reported.
File file = new File ("data/com. test. storage/info.txt ");
If (! File. exists ()){
File. createNewFile ();
}
FileOutputStream fos = new FileOutputStream (file );
Fos. write (name + "" + password). getBytes ());
Fos. flush ();
Fos. close ();
}
// Create a dialog box with parameters: display time of context content text
Toast. makeText (this, "Logon successful", Toast. LENGTH_SHORT). show ();
}
Public void readInfo () throws IOException {
File file = new File ("data/com. test. storage/info.txt ");
If (file. exists ()){
FileInputStream FCM = new FileInputStream (file );
BufferedReader br = new BufferedReader (new InputStreamReader (FCM ));
String line = null;
Line = br. readLine ();
String [] userInfo = line. split ("");
Et_name.setText (userInfo [0]);
Et_password.setText (userInfo [1]);
}
}
}
When the internal storage space of the mobile phone is insufficient, the files in the Cache will be deleted. Do not store important information in the Cache. Images on the network can be saved in the Cache.
Clear Cache is to clear all content in the Cache folder.
Clearing data is to clear all content in the independent space of the application, including the cache.
External Storage space
Sdcard: mobile hard drive equivalent to a computer
Sdcard location:
Before Android, The sdcard path is: The sdcard folder under the root directory
Before Android, sdcard path: mnt/sdcard
Start with Android4.3. sdcard path: storage/sdcard
To be compatible with earlier versions of the program, Android left a shortcut in the sdcard directories of previous versions, pointing to storage/sdcard
To write files in an external bucket, you need to add permissions. Read data in sdcard without permission.
If you select the option to protect sdcard, the permission is also required to read the data on the sdcard file.
Android. permission. READ_EXTERNAL_STORAGE |
Package com. test. storage;
Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Environment;
Import android. view. View;
Import android. widget. CheckBox;
Import android. widget. EditText;
Import android. widget. Toast;
Public class MainActivity extends Activity {
Private EditText et_name = null;
Private EditText et_password = null;
String name = "";
String password = "";
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
// The initialization of global variables, preferably in the onCreate Method
// Do not obtain the value of the component in advance, which may cause the value to change or be empty next time.
/*
* Et_name = (EditText) this. findViewById (R. id. name); et_password =
* (EditText) this. findViewById (R. id. password); name =
* Et_name.getText (). toString (). trim (); password =
* Et_password.getText (). toString (). trim ();
*/
Et_name = (EditText) this. findViewById (R. id. name );
Et_password = (EditText) this. findViewById (R. id. password );
Try {
ReadInfo ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Public void login (View v) throws IOException {
Name = et_name.getText (). toString (). trim ();
Password = et_password.getText (). toString (). trim ();
CheckBox cb = (CheckBox) this. findViewById (R. id. check );
// Determine whether the checkbox is selected
If (cb. isChecked ()){
// This method is not good. If the package name changes, an exception is reported.
// File file = new File ("sdcard/info.txt ");
// Before using sdcard, you must check the status of the sdcard.
/**
* MEDIA_REMOVED: The sdcard does not exist. MEDIA_UNKNOWN: the SDK card cannot be recognized.
* MEDIA_UNMOUNTED: sd exists, but MEDIA_MOUNTED is not mounted. The sdcard is mounted and can be used properly.
* MEDIA_MOUNTED_READ_ONLY: sdcard read-only MEDIA_CHECKING: sdcard is preparing
*/
If (Environment. getExternalStorageState (). equals (
Environment. MEDIA_MOUNTED )){
// It is best to use Context to obtain the Context information and return the real path of the sdcard.
File file = new File (Environment. getExternalStorageDirectory (),
"Userinfo.txt ");
If (! File. exists ()){
File. createNewFile ();
}
FileOutputStream fos = new FileOutputStream (file );
Fos. write (name + "" + password). getBytes ());
Fos. flush ();
Fos. close ();
}
} Else {
Toast. makeText (this, "sdcard cannot be used", Toast. LENGTH_SHORT );
}
// Create a dialog box with parameters: display time of context content text
Toast. makeText (this, "Logon successful", Toast. LENGTH_SHORT). show ();
}
Public void readInfo () throws IOException {
// File file = new File ("sdcard/info.txt ");
If (Environment. getExternalStorageState (). equals (
Environment. MEDIA_MOUNTED )){
File file = new File (Environment. getExternalStorageDirectory (),
"Userinfo.txt ");
If (file. exists ()){
FileInputStream FCM = new FileInputStream (file );
BufferedReader br = new BufferedReader (new InputStreamReader (
));
String line = null;
Line = br. readLine ();
String [] userInfo = line. split ("");
Et_name.setText (userInfo [0]);
Et_password.setText (userInfo [1]);
}
}
}
}