One, save the file to the phone memory
/**
* Save the data to the cell phone rom file.
* @param context Application contexts provide environment
* @param name username
* @param password Password
* @throws Exception * *
public
St atic void Savetorom (context context, string name, string password) throws exception{//file
file = new file ("/data/da Ta/com.itheima.login/files/info.txt ");
File File = new file (Context.getfilesdir (), "info.txt");//The file is under the Files folder under Data Getcachedir () under the cache folder size not exceeding 1Mb
FileOutputStream fos = new FileOutputStream (file);
String txt = name+ ":" +password;
Fos.write (Txt.getbytes ());
Fos.flush ();
Fos.close ();
}
/**
* Gets the saved data
* @param context
* @return
/public static map<string,string> GetUserInfo ( Context context) {
File File = new file (Context.getfilesdir (), "Info.txt");
try {
FileInputStream fis = new FileInputStream (file);
You can also directly read the file string result = Streamtools.readfromstream (FIS);
BufferedReader br = new BufferedReader (new InputStreamReader (FIS));
String str = br.readline ();
string[] Infos = Str.split (":");
map<string,string> map = new hashmap<string, string> ();
Map.put ("username", infos[0]);
Map.put ("Password", infos[1]);
return map;
} catch (Exception e) {
e.printstacktrace ();
return null;
}
}
Finally, the above method can be directly invoked to read the information
map<string, string> Map = GetUserInfo (this);
If (map!=null) {
textview.settext (map.get ("username"));
}
Second, save the file to the SD card
to get the size of the cell phone SD space:
File path = Environment.getexternalstoragedirectory ();
Statfs stat = new Statfs (Path.getpath ());
Long blockSize = Stat.getblocksize ();
Long totalblocks = Stat.getblockcount ();
Long availableblocks = Stat.getavailableblocks ();
Long totalsize = blocksize*totalblocks;
Long availsize = blockSize * availableblocks;
String totalstr = formatter.formatfilesize (this,totalsize);
String availstr = Formatter.formatfilesize (this, availsize);
Tv.settext ("Total space" +totalstr+ "\ n" + "free Space" +availstr);
To add permission to write external storage:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> public static void Save (string name, string password) throws exception{if (Environment.getexternalstoragestate (). equals (environment.media_mounted)) {File File = new file (Environment.getexternalstoragedirectory (), "info.txt");///write directly/sdcard/info.txt to determine if SD card exists Fileo
Utputstream fos = new FileOutputStream (file);
String txt = name+ ":" +password;
Fos.write (Txt.getbytes ());
Fos.flush ();
Fos.close ();
Using randomaccessfile like file append content FileOutputStream will empty the original file contents//randomaccessfile RAF = new Randomaccessfile (file, "RW"); Raf.seek (File.length ());
Move the file pointer to the last//raf.write (Name.getbytes () +password.getbytes ());
Raf.close (); }
}
Read file add Read permission public
static String read () {
try {
if (environment.getexternalstoragestate (). Equals ( environment.media_mounted)) {
File sdcarddir = Environment.getexternalstoragedirectory ();
FileInputStream fis = new FileInputStream (Sdcarddir.getcanonicalpath () + "Info.txt");
BufferedReader br = new BufferedReader (new InputStreamReader (FIS));
StringBuilder sb = new StringBuilder ("");
String line = null;
while (line = Br.readline ())!= null) {
sb.append (line);
return sb.tostring ();
}
catch (Exception e) {
e.printstacktrace ();
}
return null;
}
Third, the use of sharedpreferences
sharedpreference is commonly used in the development of a storage mode, mainly store some of the system invariant parameters such as whether it is the first time into the application, and so on, through the way of key-value pair storage
Types that can be stored: booleans, floats, ints, longs,strings.
Getsharedpreferences ()-Storing multiple parameters
Getpreferences ()-stores only one parameter and does not need to specify a name (key)
Steps to write:
Sharedpreferences call edit () to get a editor object
Add a value using Putboolean () and putstring ()
Commit TRANSACTION Completion Storage
READ: Just call Sharedpreferences Getboolean () and getString ()
Here is the sample code:
public class Mysharedpreference {
private context context;
Private Sharedpreferences sp;
Private Editor edit;
Public Mysharedpreference {
This.context = context;
}
public boolean savemessage (String name,string pwd) {
Boolean flag = false;
SP = context.getsharedpreferences ("UserInfo", context.mode_private);
Mode defines the access right now that this application can access
edit = Sp.edit ();
Edit.putstring ("name", name);
Edit.putstring ("pwd", pwd);
Flag = Edit.commit ()//commits the transaction to persist the data to the storage return
flag
;
Public map<string,object> GetMessage () {
map<string,object> Map = new hashmap<string, object> () ;
SP = context.getsharedpreferences ("UserInfo", context.mode_private);
String name = sp.getstring ("name", "");
String pwd = sp.getstring ("pwd", "");
Map.put ("name", name);
Map.put ("pwd", pwd);
return map;
}