Android – File storage

“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

File storage is one of the most basic data storage in Android, it does not store the format of the content to do any processing, so the data is intact save in the file, so it is suitable for storing some simple text data, if you want to use file storage way to save some of the more complex text data, You need to define your own format range so that you can re-parse the data out of the text later.

1. Store the data in a file

The Content class provides an openFileOutput () method to store data to a specified file. This method can take two parameters, the first is the file name, the default file storage path is data/data//files/ directory, the second parameter is the file operation mode, there are two main operation modes, MODE_PRIVATE and MODE_APPEND. MODE_PRIVATE is the default operation mode. When a file has content, the written content overwrites the content of the original file. In MODE_APPEND mode, the content is appended to the end of the file. Create a new file if it does not exist. The OpenFileOutput () method returns a FileOutputStream object, which can be used to write data to a file as a Java stream. The following code shows how to save a piece of text to a file:

public void save(a){
    String data = "Data to save";
    FileOutputStream out = null;
    BufferedWriter write =null;
    try {
        out = openFileOutput("data", Context.MODE_PRIVATE);
        write = new BufferedWriter(new OutputStreamWriter(out));
        write.write(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if(write ! =null){ write.close(); }}catch(IOException e) { e.printStackTrace(); }}}Copy the code

We build a FileOutputStream using the openFileOutput () method, and then use it to build an OutputStreamWriter. We then use OutputStreamWriter to build a BufferedWriter object so that we can write text to a file using The BufferedWriter.

2. Read data from a file

Similar to storing data in a file, the context class also provides an openFileInput () method that reads data from a file. This method is a bit simpler than openFileOutput (). It takes only one argument, the name of the file to read, and the system automatically loads the file in the default directory. And returns a FileInputStream object, which is then read as a Java stream. Here is a code that shows how to read data from a file:

public String loda(a){
    FileInputStream in = null;
    BufferedReader reader = null;
    StringBuilder content = new StringBuilder();
    try {
        in = openFileOutput("data");
        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        while((line = reader.readLine())! =null){ content.append(line); }}catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(reader ! =null) {try {
                reader.close();
            } catch(IOException e) { e.printStackTrace(); }}}return content.toString();
}
Copy the code

We build a FileInputStream using the openFileInput () method, and then we build an InputStreamWriter using it, and then we build a BufferedReader using InputStreamReader, So you can read it line by line with a BufferedReader, get all the text out of the file, put it in a StringBuilder object, and then return it.