Working with Files and Directories Android

Remember from Chapter “Introducing Android,” that each Android application is its own user on the underlying Linux operating system. It has its own private application directory and files. Within the Android SDK, you can also find a variety of standard Java file utility classes (such as java.io) for handling different types of files, such as text files, binary files, and XML files.

In Chapter “Managing Application Resources,” you also learned that Android applications can also include static raw and XML files as resources. Although retrieving the file is handled slightly differently when accessing resources, the file can be read like any other file.

Android application files are stored in a standard directory hierarchy on the Android file system. You can browse an application’s directory structure using the DDMS File Explorer.

Exploring with the Android Application Directories

Android application data is stored on the Android file system in the following top-level directory:

Several default subdirectories are created for storing databases, preferences, and files as necessary. You can also create other custom directories as needed. File operators all begin by interacting with the application Context object. Table lists some important methods available for application file management. You can use all the standard java.io package utilities to work with FileStream objects and such.

Important android.content.Context File and Directory Management Methods

Important android.content.Context File and Directory Management Methods

Creating and Writing to Files to the Default Application Directory

Android applications that require only the occasional file rely upon the helpful method called openFileOutput(). Use this method to create files in the default location under the application data directory:

For example, the following code snippet creates and opens a file called Filename.txt. We write a single line of text to the file and then close the file:

We can append data to the file by opening it with the mode set to MODE_APPEND:

Reading from Files in the Default Application Directory

Again we have a shortcut for reading files stored in the default /files subdirectory. The following code snippet opens a file called Filename.txt for read operations:

Reading Raw Files Byte-by-Byte

You handle file-reading and -writing operations using standard Java methods. Check out the subclasses of java.io.InputStream for reading bytes from different types of primitive file types. For example, DataInputStream is useful for reading one line at a time.

Here’s a simple example of how to read a text file, line by line, and store it in a StringBuffer:

Reading XML Files

The Android SDK includes several utilities for working with XML files, including SAX, an XML Pull Parser, and limited DOM, Level 2 Core support. Table lists the packages helpful for XML parsing on the Android platform.

Important XML Utility Packages

Important XML Utility Packages

Working with Other Directories and Files on the Android File System

Using Context.openFileOutput() and Context.openFileInput() are great if you have a few files and you want them stored in the /files subdirectory, but if you have more sophisticated file-management needs, you need to set up your own directory structure. To do this, you must interact with the Android file system using the standard java.io.File class methods.

The following code gets a File object for the /files application subdirectory and retrieves a list of all filenames in that directory:

Here is a more generic method to create a file on the file system. This method works anywhere on the Android file system you have permission to access, not the /files directory:

You can use File objects to manage files within a desired directory and create sub directories. For example, you might want to store “track” files within “album” directories. Or perhaps you want to create a file in a directory other than the default.

Let’s say you want to cache some data to speed up your application’s performance and how often it accesses the network. In this instance, you might want to create a cache file. There is also a special application directory for storing cache files. Cache files are stored in the following location on the Android file system:

The following code gets a File object for the /cache application subdirectory, creates a new file in that specific directory, writes some data to the file, closes the file, and then deletes it:



All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics