FileInputStream and Android database tools for android Data Storage

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.