use file files for data storage in Android
Last learned to use sharedperences for data storage, then learn to use file for storage we sometimes can save the data directly in the form of a file in the device,
Example: text files, picture files, etc.
Using file for storage operations mainly uses the following
①:p ublic abstract FileInputStream openfileinput (String name)
This is mostly open file, return FileInputStream
②:p ublic abstract fileoutputstream openfileoutput (String name, int mode)
This is mostly written to the file, if the file does not exist, directly to create, return FileOutputStream
Mode (mainly with the following four types of modes)
Mode_append//Append at tail
Mode_private//Private
Mode_world_readable//readable
Mode_world_writeable//Writable
③:fileinputstream (get file input stream) and FileOutputStream (get file output stream) These two classes are common in Javaio operations
After the operation is saved successfully, the file is saved in the current files/of the program's package name (other paths that can be changed for storage)
The source code is as follows:
[Java]View Plaincopy
- Package com.jiangqq.file;
- Import Java.io.FileInputStream;
- Import Java.io.FileOutputStream;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.widget.Button;
- Import Android.widget.EditText;
- Import Android.widget.Toast;
- Public class Fileacitivy extends Activity
- {
- private Button bt1, BT2;
- private EditText et1, Et2;
- private static final String FILENAME = "Temp_file.txt";
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- BT1 = (Button) This.findviewbyid (R.ID.BT1);
- BT2 = (Button) This.findviewbyid (R.ID.BT2);
- Et1 = (EditText) This.findviewbyid (R.ID.ET1);
- Et2 = (EditText) This.findviewbyid (R.id.et2);
- Bt1.setonclicklistener (new Onclicklistener () {
- @Override
- public void OnClick (View v) {
- Write (Et1.gettext (). toString ());
- Toast.maketext (Fileacitivy. This, "file write succeeded", Toast.length_long)
- . Show ();
- }
- });
- Bt2.setonclicklistener (new Onclicklistener () {
- @Override
- public void OnClick (View v) {
- Et2.settext (read ());
- Toast.maketext (Fileacitivy. This, "file read success", Toast.length_long)
- . Show ();
- }
- });
- }
- //Read File method
- private String Read () {
- try {
- FileInputStream InputStream = Openfileinput (FILENAME);
- byte[] B = new byte[inputstream.available ()];
- Inputstream.read (b);
- return new String (b);
- } catch (Exception e) {
- }
- return null;
- }
- //write file
- private void Write (String content) {
- try {
- FileOutputStream fos = openfileoutput (FILENAME, mode_append);
- Fos.write (Content.getbytes ());
- Fos.close ();
- } catch (Exception e) {
- }
- }
- }
Source code Download Link:
http://115.com/file/c2mu1pfd#
Activity_18_file.rar
Original: http://blog.csdn.net/developer_jiangqq/article/details/7039852
Use file files for data storage in Android