Three of Android's typical local data storage methods
Sharedpreferences
Save in a file format in local storage
SQL database
This article explains how to save a file using Sharedpreferences. The main explanation is what is written in the annotation.
Ide:android Studio
Reference article: http://www.jb51.net/article/74215.htm
Ramble about: Originally file operation this piece wants to understand it last week, then continue to study further. But because the concept of the official Android training is too complicated. Leads me to think of storing it inside to query the space and get back to something like that. The result is that I confuse the concept of an external space (storage space) with a file folder that is stored in the internal space (the name of the package under the Data directory). So a lot of time has been delayed and no law. Finally see the reference article to know how to write. Then I followed the reference article.
Similarly, I used the separation of the writing, that is, to create a tool class, modular easy to use. Hope to help others, but also to create a concept for themselves.
Words not much said, on the code:
Import Android.content.Context;
Import Android.util.Log;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
/** * Created by Zhizhao on 2015/11/1 0001 in 16:00.
* * 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.f
Ilecontext = context;
This.fileusername = UserName;
This.filepassword = password;
The///save is written continuously in the contents of the file, which is written again on the basis of 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 ();}} Reading a file reads the contents of the entire file.
If you want to add a resolution, save it in 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 ("read the file as:", "" "+new String (buffer));
Fileinputstream.close ();
catch (Exception e) {e.printstacktrace ();}} }
The following is used:
private void WriteFile () {
Usingfileinputstream fileinputstream = new Usingfileinputstream ("Account",
Mysavedataactivity.this, UserName, userpass);
Fileinputstream.writefileinputstream ();
Tvreadinformation.settext ("Save success!") "+" \ n UserName = "+ UserName +" \ userpass = "+ Userpass);
}
private void ReadFile () {
Usingfileinputstream fileinputstream = new Usingfileinputstream ("Account",
Mysavedataactivity.this, UserName, userpass);
Fileinputstream.readfileinputstream ();
Tvreadinformation.settext ("read successful!") "+" \ n UserName = "+username+" \ Userpass = "+userpass);
}
To sum up:
I don't think I'm writing right now, it's perfect. Because in the call process to repeatedly fill in the file name, passed the value. And it's hard to know success fails in the return value.
Moreover I did not have the file exception capture and carries on the operation, because if does not have the file the situation to proceed the operation, certainly will have the error: null pointer exception.
However, since the practice is not considered so much, because this time as long as the Try{}catch () {} code block to add their own means of operation.
Here's a little time to introduce the Android FileInputStream class.
1.FileInputStream class overview
Inheritance Relationship:
Java.io.fileinputstream->java.io.inputstream->java.lang.object
To implement an interface:
Closeable
Functions of the class:
FileInputStream gets the input bytes from a file in the file system. Which files are available depends on the host environment.
FileInputStream is used to read raw byte streams such as image data. To read character streams, consider using FileReader.
2. Properties and behavior of class
(1) public void Close () throws IOException
Function: Closes this file input stream and releases all system resources associated with this stream.
If the stream has a channel associated with it, the channel is closed.
Specified by: Close in interface closeable
Overwrite: Close in class InputStream
Thrown: IOException-If an I/O error occurs.
(2) public int read () throws IOException
Function: Reads a byte of data from this input stream. If no input is available, this method blocks.
Specified by: Read in class InputStream
Returns: The next data byte, or 1 if the end of the file has been reached.
Thrown: IOException-If an I/O error occurs.
(3) public int read (byte[] b) throws IOException
Function: Reads up to b.length bytes of data from this input stream into a byte array. This method blocks before some input is available
Overwrite: Read in class InputStream
Parameters: B-buffer to store read data
Returns: The total number of bytes read into the buffer, or 1 if the end of the file has been reached and there is no more data.
Thrown: IOException-If an I/O error occurs.
(4) public int read (byte[] b, int off, int len) throws IOException
Function: Reads up to len bytes of data from this input stream into a byte array. This method will block until some input is available.
Overwrite: Read in class InputStream
Parameters: B-stores a buffer for reading data.
Off-The starting offset of the data.
Len-The maximum number of bytes read.
Returns: The total number of bytes read into the buffer, or 1 if the end of the file has been reached and there is no more data.
Thrown: IOException-If an I/O error occurs.
3. Common Mistakes
Using FileInputStream under Eclipse prompts you not to find the specified file
Code:
filename = "Abc.txt";
FileInputStream fis = new FileInputStream (filename);
Error display:
Java.io.FileNotFoundException:dbconfig.properties (the system cannot find the specified file.) ) at
Java.io.FileInputStream.open (Native method) at
java.io.fileinputstream.<init> ( fileinputstream.java:106) at
java.io.fileinputstream.<init> (fileinputstream.java:66)
Workaround:
Because eclipse will automatically publish the directory as its root when running the main program under Eclipse, you will be prompted to not find the file and change the filename to an absolute directory
filename = "\sdcard\...\abc.txt";