Dabbling in Android: File Read and Write


Introduction

There's lots of information out there on Android about reading and writing files. It includes buffers and streams and no end of object types to get things working. In order to counteract this, what I came up with here is a beginner's way to read and write to files:
File f = new File(this.getFilesDir(), "hello.txt");
        f.createNewFile();
if (f.exists()) {
    System.out.println("Status: File Created");

        FileWriter fw = new FileWriter(f);
        fw.write("hello world");
        fw.close();

        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);
        String s;
        while((s = br.readLine()) != null) {
            System.out.println("File contents: "+s);
        }
        fr.close();
        f.delete();
        if (!f.exists()) {
            System.out.println("Status: File Deleted");
        }
}
        else System.out.println("Status: File Creation Failed!");

File

So let's break this down. First we create a File instance, which in Android speak is:
An "abstract" representation of a file system entity identified by a pathname. The pathname may be absolute (relative to the root directory of the file system) or relative to the current directory in which the program is running.
The actual file referenced by a File may or may not exist. It may also, despite the name File, be a directory or other non-regular file. (Android Developer)
And here is that File instance:
File f = new File(this.getFilesDir(), "hello.txt");
It uses the current context (this) to retrieve a path to the current app-specific file directory and appends our chosen filename. But this isn't an actual file yet, it is "an 'abstract' representation ... identified by a pathname". So now we create the file using the createFile() instance method:
f.createNewFile();

FileWriter

Having created a file, and then confirmed its existence, we use another class (FileWriter) to write to that file:
FileWriter fw = new FileWriter(f);
fw.write("hello world");
Once the writing is finished the file is then closed:
fw.close();

FileReader

Now the file exists and contains a string it is time to read the file.
FileReader fr = new FileReader(f);      
The first step in the reading process is to create a FileReader instance. And while we could use the read() method of the FileReader superclass (Reader) to transfer the contents of the file into a character buffer it is recommended the less expensive method of using a BufferReader instance is employed.

BufferReader

BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null) {
System.out.println("File contents: "+s);
}     
After the BufferReader instance has read the file, it is time to close the file reader and, for demonstration purposes, delete the file.
fr.close();
f.delete();

Conclusion

It's my intention that this post will grow into a series that will look at simplifying and demystifying Android, because from my personal experience all too often searches for information about how to write code for Android return large chunks of code and few explanations.

Endorse on Coderwall

Comments