FileInputStream and Android database tools for android Data Storage
Three typical local data storage methods for Android
Import android. content. Context; Import android. util. Log;
Import java. io. FileInputStream; Import java. io. FileOutputStream;
/** * Created by zhizhao on 0001 in. */ Public class UsingFileInputStream { Private Context fileContext; Private String fileName; Private String fileUserName; Private String filePassword;
Public UsingFileInputStream (String name, Context context, String userName, String password ){ This. fileName = name; This. fileContext = context; This. fileUserName = userName; This. filePassword = password; } // The data is written continuously in the file content during storage, that is, the data is written again based on the previously saved data. Public void writeFileInputStream (){ Try { FileOutputStream fileOutputStream = fileContext. openFileOutput (fileName, FileContext. MODE_PRIVATE ); Byte [] byteUserName = fileUserName. getBytes (); Byte [] bytePassword = filePassword. getBytes (); FileOutputStream. write (byteUserName ); FileOutputStream. write (bytePassword ); Log. v ("FileInputStream save result", "UserName =" + fileUserName + "Password =" + filePassword ); FileOutputStream. flush (); FileOutputStream. close (); } Catch (Exception e ){ E. printStackTrace (); }
} // Read the entire file. If you want to add resolution, save it as a special format. Public void readFileInputStream (){ Try { FileInputStream fileInputStream = fileContext. openFileInput (fileName ); Int len = fileInputStream. available (); Byte [] buffer = new byte [len]; FileInputStream. read (buffer ); Log. v ("the File Read is:", "" + new String (buffer )); FileInputStream. close (); } Catch (Exception e ){ E. printStackTrace (); } } } |
The following is the usage:
Private void writeFile (){ UsingFileInputStream fileInputStream = new UsingFileInputStream ("account ", MySaveDataActivity. this, userName, userPass ); FileInputStream. writeFileInputStream (); TvReadInformation. setText ("saved successfully! "+" \ N UserName = "+ userName +" \ n UserPass = "+ userPass ); }
Private void readFile (){ UsingFileInputStream fileInputStream = new UsingFileInputStream ("account ", MySaveDataActivity. this, userName, userPass ); FileInputStream. readFileInputStream (); TvReadInformation. setText ("read successful! "+" \ N UserName = "+ userName +" \ n UserPass = "+ userPass ); }
|
Summary:
I don't think I am writing right now. It's perfect. This is because you need to fill in the file name repeatedly during the call process and input the value. In addition, it is difficult to know the success or failure in the returned values.
Besides, I did not capture and operate the file exceptions, because if there is no file, the system will inevitably report an error: NULL pointer exception.
However, since it is a practice, it is not considered so much, because in this case, you only need to add your own operation methods in the try {} catch () {} code block.
Good night.