Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Getting File and Directory Information

Published? true
FormatLanguage: WikiFormat

Problem:

You need to know all you can about a given file "on disk", typically on internal memory or on the SD Card.

Solution:

Use a java.io.File object.

Discussion:

The File class has a number of 'informational' methods. To use any of these, you must construct a File object containing the name of the file it is to operate upon. It should be noted up front that creating a File object has no effect on the permanent filesystem; it is only an object in Java's memory. You must call methods on the File object in order to change the filesystem; as we'll see, there are numerous 'change' methods, such as one for creating a new (but empty) file, one for renaming a file, etc., as well as many informational methods. The table below lists some of the informational methods.

Return type Method name Meaning
boolean exists() True if something of that name exists
String getCanonicalPath() Full name
String getName() Relative filename
String getParent() Parent directory
boolean canRead() True if file is readable
boolean canWrite() True if file is writable
long lastModified() File modification time
long length() File size
boolean isFile() True if it's a file
boolean isDirectory() True if it's a directory (note: might be neither file nor directory)

You can not change the name stored in a File object; you simply create a new File object each time you need to refer to a different file:

import java.io.*;
import java.util.*;

/**
 * Report on a file's status in Java
 */

public class FileStatus {

	public static void main(String[] argv) throws IOException {
		// Ensure that a filename (or something) was given in argv[0]
		if (argv.length == 0) {
			System.err.println("Usage: FileStatus filename");
			System.exit(1);
		}

		for (int i = 0; i< argv.length; i++) {
			status(argv[i]);
		}
	}

	public static void status(String fileName) throws IOException {
		System.out.println("---" + fileName + "---");

		// Construct a File object for the given file.
		File f = new File(fileName);

		// See if it actually exists
		if (!f.exists()) {
			System.out.println("file not found");
			System.out.println(); // Blank line
			return;
		}

		// Print full name
		System.out.println("Canonical name " + f.getCanonicalPath());

		// Print parent directory if possible
		String p = f.getParent();
		if (p != null) {
			System.out.println("Parent directory: " + p);
		}

		// Check our permissions on this file
		if (f.canRead()) {
			System.out.println("File is readable by us.");
		}
		// Check if the file is writable
		if (f.canWrite()) {
			System.out.println("File is writable by us.");
		}

		// Report on the modification time.
		Date d = new Date();
		d.setTime(f.lastModified());
		System.out.println("Last modified " + d);

		// See if file, directory, or other. If file, print size.
		if (f.isFile()) {
			// Report on the file's size
			System.out.println("File size is " + f.length() + " bytes.");
		} else if (f.isDirectory()) {
			System.out.println("It's a directory");
		} else {
			System.out.println("So weird, man! Neither a file nor a directory!");
		}

		System.out.println(); // blank line between entries
	}
}

When run with the three command line arguments shown, it produces the output below. These examples are drawn from desktop systems, but the API works the same on Android as it does anywhere Java goes.

C:\javasrc\dir_file>'''java FileStatus / /tmp/id /autoexec.bat'''
---/---
Canonical name C:\
File is readable.
File is writable.
Last modified Thu Jan 01 00:00:00 GMT 1970
It's a directory

---/tmp/id---
file not found

---/autoexec.bat---
Canonical name C:\AUTOEXEC.BAT
Parent directory: \
File is readable.
File is writable.
Last modified Fri Sep 10 15:40:32 GMT 1999
File size is 308 bytes.

As you can see, the so-called canonical name not only includes a leading directory root of C:\ , but also has had the name converted to uppercase. You can tell I ran that on an older version of MS-Windows. On Unix, it behaves differently:

$ '''java FileStatus / /tmp/id /autoexec.bat'''
---/---
Canonical name /
File is readable.
Last modified October 4, 1999 6:29:14 AM PDT
It's a directory

---/tmp/id---
Canonical name /tmp/id
Parent directory: /tmp
File is readable.
File is writable.
Last modified October 8, 1999 1:01:54 PM PDT
File size is 0 bytes.

---/autoexec.bat---

file not found

$

This is because a typical Unix system has no autoexec.bat file. And Unix filenames (like those on the filesystem inside your Android device, and those on a Mac) can consist of upper- and lowercase characters: what you type is what you get.