The Way to Advance Android - File Storage in Four Storage Ways

Keywords: Android Java Mobile less

When it comes to storage in Android, you should all think of file storage in mobile phones, sd cards.
Storage, sp storage and data storage, which I have written before, but the editors were messy and inconvenient, so now I rewrite the four storage methods and explain them. This book is about the storage and reading of files, less gossip, Code sent.

UI Map:

MainActivity Code:

package com.example.storagedispose;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    private EditText mEdit;
    private TextView mContent;
    private FileOutputStream openFileOutput;
    private BufferedWriter bufferedWriter;
    private FileInputStream openFileInput;
    private BufferedReader bufferedReader;
    private StringBuilder builderContent;
    private String data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

    }

    private void initView() {
        mContent = (TextView) findViewById(R.id.main_content);
        mEdit = (EditText) findViewById(R.id.main_edit);
        Button mSave = (Button) findViewById(R.id.save_one);
        Button mLoad = (Button) findViewById(R.id.load_one);

        mSave.setOnClickListener(this);
        mLoad.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.save_one:
            data = mEdit.getText().toString();
            onsave(data);
            break;
        case R.id.load_one:
            onload();
            break;
        default:
            break;
        }
    }

    // The total exception thrown directly by reading the file
    private void onload() {
        // Read the file named -- save_one
        try {
            openFileInput = openFileInput("save_one");

            // Character Read Stream
            bufferedReader = new BufferedReader(new InputStreamReader(
                    openFileInput));
            // Create StringBuilder for storing data
            builderContent = new StringBuilder();
            // Check to see if there is any data, or add it directly to our container
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                builderContent.append(line);
                mContent.setText(builderContent);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    // File Storage
    private void onsave(String data) {

        // Store data in local files
        try {
            // Store data in a specified file - the first parameter is the file name, and the second parameter determines how to write
            openFileOutput = openFileOutput("save_one", MODE_PRIVATE);
            // Create a character input stream. Write in the file content above it.
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(
                    openFileOutput));
            // Save the data we entered into this folder
            bufferedWriter.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // The execution here is entirely to avoid memory leaks
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

MainActivity Xml Code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/main_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Enter the information to be saved here"
             />

        <Button
            android:id="@+id/save_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="file save" />
    </LinearLayout>

    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/load_one"
        android:text="Read file data"
        android:gravity="center"
        />
    <TextView
        android:id="@+id/main_content"
        android:text="Read data display area"
        android:gravity="center"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp" />

</LinearLayout>

end map:
1. Here we enter what we want to save, and then click Save.

2. Export data through DDMS and query data

3. Look at the exported data (we found that there is no problem with the data, oh, write successfully here)

4. What we are using now is data reading (the display area is no problem).

How to use it:
1. Using openFileOutput storage, openFileInput reads
2. Write when input, so use Output, read when output, use Input.
3. Use Buffered Write and Buffered Read, and then pass in FileOutputStream and FileInputStream above.
4.BufferedRead because to read, all containers that remember StringBuilder
5. Remember close to avoid memory leaks

Be careful:

Remember to add read and write permissions

 <uses-permission android:name="android.permission.WRITE_PROFILE"/>
    <uses-permission android:name="android.permission.READ_PROFILE"/>

File storage sometimes can not meet our needs, because he has only one text information, I think more is a text display, such as some user's instructions, etc. The next chapter brings Sp storage and reading, you can skip through the following links to view.

Posted by JoCitizen on Mon, 25 Mar 2019 22:48:28 -0700