Introduction to files and directories

The associated package File:

  • java.io.File

One constructor for the File class:

Constructors Description
file(StringPath) Creates a File Object that refers to the specified path name.

Methods that check a File object:

Method Description
exists() Tests whether the file or directory denoted by this abstract pathname exists.
canRead() Tests whether the application can read the file denoted by this abstract pathname
canWrite() Tests whether the application can modify the file denoted by this abstract pathname
isDirectory() Tests whether the file denoted by this abstract pathname is a directory.
isFile() Tests whether the file denoted by this abstract pathname is a normal file

Mehtods that get information about a File object:

Method Description
getName() Returns the name of the file or directory denoted by this abstract pathname
getPath() Converts this abstract pathname into a pathname string
getAbsolutePath() Returns the absolute pathname string of this abstract pathname
length() Returns the length of the file denoted by this abstract pathname
lastModified() Returns the time that the file denoted by this abstract pathname was last modified
listRoots() List the available filesystem roots.
listFiles() Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname
list() Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname
listFiles(FileFilter filter)
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

Mehtods that work with File objects:

Method Description
setReadable (boolean readable) A convenience method to set the owner's read permission for this abstract pathname
setReadOnly() Marks the file or directory named by this abstract pathname so that only read operations are allowed
createNewFile() Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist
mkdirs() Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories
delete() Deletes the file or directory denoted by this abstract pathname

Description:

  • when coding directorynames, you can use a front slash to separate directories. (All code windows compatable)
  • To identify the name and location of a file, you can use an absolute path name to specify the entire path for a file.

Example 1: Code that creates a directory if it doesn't already exist

String dirName = "C:/java/files/";
File dir = new File(dirName);
if (!dir.exists());
dir.mkdirs();

Example 2: Code that creates a file if it doesn't already exist

String fileName = "products.txt";
File productsFile = new File(dirName + fileName);
if (!productsFile.exists());
productsFile.createNewFile();

Example 3: Code that displayes information about a file

System.out.println("File name: " + productsFile.getName());
System.out.println("Absolute path: " + productsFile.getAbsolutePath());
System.out.println("Is writeable: " + productsFile.canWrite());

Example 4: Code that displays information about a directory

If (dir.exists() && dir.isDirectory())
{
System.out.println("Directory: " + dir.getAbsolutePath());
System.out.println("Files: ");
for (String filename : dir.list())
System.out.println(" : + filename);
}

Example 5: code that specifies a directory and file on a remote server

String dirName = "//server/c/editorial/customers.txt";

More info can be found here: http://www.j2ee.me/javase/6/docs/api/java/io/File.html#File(java.lang.String)