Android development Note (17) data storage 2 file storage data, android file storage

Source: Internet
Author: User

Android development Note (17) data storage 2 file storage data, android file storage

Android provides five data storage methods:

1. SharedPreferences

2. File Storage

3. SQLite Database

4. ContentProvider

5. Network Storage

 

This document describes how to use files to store data. Android file operations use the FileOutputStream and FileInputStream classes in Java. IO.

I. File Storage

First, instantiate a FileOutputStream.

FileOutputStream foStream = openFileOutput (fileName, MODE_PRIVATE);
// fileName: name of the file to write
// MODE_PRIVATE: is the default operation mode, which means that the file is private data and can only be accessed by the application itself. In this mode, the content written will overwrite the content of the original file
// MODE_APPEND: The mode will check if the file exists, add content to the file if it exists, otherwise create a new file.
// MODE_WORLD_READABLE: indicates that the current file can be read by other applications, it is not recommended
// MODE_WORLD_WRITEABLE: indicates that the current file can be written by other applications, it is not recommended

Then, you can call foStream. write () to complete writing.

byte[] buffer = fileContent.getBytes();
foStream.write(buffer);
Toast.makeText(MainActivity.this, "写入成功",Toast.LENGTH_SHORT).show();

Finally, some cleanup work is performed to refresh the written stream and close the stream.

foStream.flush();
foStream.close();

Ii. Reading files

Similarly, a FileInputStream is instantiated first.

FileInputStream fiStream = openFileInput(fileName);

Call fiStream. read ().

int len = fiStream.available();
byte[] buffer = new byte[len];
fiStream.read(buffer);

Finally, the text is displayed and the Read File stream is closed.

etContent.setText (new String (buffer));
Toast.makeText (MainActivity.this, "Read successfully", Toast.LENGTH_SHORT) .show ();
fiStream.close ();

 

Iii. complete code

 1 import android.support.v7.app.AppCompatActivity;
 2 import android.os.Bundle;
 3 import android.view.View;
 4 import android.widget.Button;
 5 import android.widget.EditText;
 6 import android.widget.Toast;
 7 
 8 import java.io.FileInputStream;
 9 import java.io.FileOutputStream;
10 
11 public class MainActivity extends AppCompatActivity {
12 
13     private EditText etName;
14     private EditText etContent;
15     private Button btnWrite;
16     private Button btnRead;
17     private String fileName = "";
18     private String fileContent = "";
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24 
25         etName = (EditText)findViewById(R.id.etName);
26         etContent = (EditText)findViewById(R.id.etContent);
27         btnWrite = (Button)findViewById(R.id.btnWrite);
28         btnRead = (Button)findViewById(R.id.btnRead);
29 
30         btnWrite.setOnClickListener(new View.OnClickListener() {
31             @Override
32             public void onClick(View view) {
33                 fileName = etName.getText().toString();
34                 fileContent = etContent.getText().toString();
35                 try {
36                     FileOutputStream foStream = openFileOutput(fileName, MODE_PRIVATE);
37                     byte[] buffer = fileContent.getBytes();
38                     foStream.write(buffer);
39                     Toast.makeText(MainActivity.this, "写入成功",Toast.LENGTH_SHORT).show();
40                     foStream.flush();
41                     foStream.close();
42                 }catch(Exception e){
43                     e.printStackTrace();
44                 }
45             }
46         });
47         btnRead.setOnClickListener(new View.OnClickListener() {
48             @Override
49             public void onClick(View view) {
50                 fileName = etName.getText().toString();
51                 try{
52                     FileInputStream fiStream = openFileInput(fileName);
53                     int len = fiStream.available();
54                     byte[] buffer = new byte[len];
55                     fiStream.read(buffer);
56                     etContent.setText(new String(buffer));
57                     Toast.makeText(MainActivity.this, "读取成功",Toast.LENGTH_SHORT).show();
58                     fiStream.close();
59                 }catch(Exception e){
60                     e.printStackTrace();
61                 }
62             }
63         });
64 
65     }
66 }
1 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" 3 android: layout_height = "match_parent" android: paddingLeft = "@ dimen/activity_horizontal_margin" 4 android: paddingRight = "@ dimen/plugin" 5 android: paddingTop = "@ dimen/activity_vertical_margin" 6 android: paddingBottom = "@ dimen/activity_vertical_margin" tools: context = ". mainActivity "> 7 8 9 <EditText10 android: layout_width =" wrap_content "11 android: layout_height =" wrap_content "12 android: id =" @ + id/etName "13 android: feature = "true" 14 android: layout_alignParentLeft = "true" 15 android: layout_alignParentStart = "true" 16 android: layout_alignParentRight = "true" 17 android: feature = "true" 18 android: text = "file name"/> 19 20 <EditText21 android: layout_width = "wrap_content" 22 android: layout_height = "wrap_content" 23 android: id = "@ + id/etContent" 24 android: layout_below = "@ + id/etName" 25 android: layout_alignParentLeft = "true" 26 android: layout_alignParentStart = "true" 27 android: layout_alignParentRight = "true" 28 android: layout_alignParentEnd = "true" 29 android: text = "file content"/> 30 31 <Button32 android: layout_width = "wrap_content" 33 android: layout_height = "wrap_content" 34 android: text = "save" 35 android: id = "@ + id/btnWrite" 36 android: layout_alignTop = "@ + id/btnRead" 37 android: layout_toLeftOf = "@ + id/btnRead" 38 android: layout_toStartOf = "@ + id/btnRead"/> 39 40 <Button41 android: layout_width = "wrap_content" 42 android: layout_height = "wrap_content" 43 android: text = "read" 44 android: id = "@ + id/btnRead" 45 android: layout_below = "@ + id/etContent" 46 android: layout_alignParentRight = "true" 47 android: layout_alignParentEnd = "true"/> 48 </RelativeLayout>Activity_main.xml

 


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.