Java Programming   Java Programming
 |Sofia Home | Content Gallery |
Home
Syllabus
Schedule
Lessons
Assignments
Resources

Lesson 13.6 Operations

File Operations
So far in this lesson, we've looked at the input and output stream classes, the ones marked in blue on the illustration shown here.

In this section, you'll look at one of the other classes presented here, the File class.
The classes in the java.io package. [Click to enlarge.]
The Java File class is designed to allow cross-platform access to the file system of your computer.

The file system is the part of the operating system that is responsible for creating the directory or structure [folders] used by your computer, for associating file names with different files, and for instituting a series of permissions or attributes attached to those files.

The Java FileStream classes allow you to work with the contents of files, while the File class allows you to work with files themselves.

Because file systems vary so widely across different platforms, the File class is only partially successful in its attempt at cross-platform portablility. The Java File class looks at the file system in a very Unix-ish way. This translates fairly well to Windows, but is really pretty far afield by the time you get to the Mac, where files consist of different forks and contain creator codes among other things.

Back to Top

File Objects

Let's first look at how you construct a File object. The File class has two constructors that are commonly used:

File(String path);
File(String path, String filename);

The first requires you to supply a fully qualified or absolute path, while the second allows you to supply the information in two pieces. The second constructor is useful because both of these pieces of information are easily retrieved from a FileDialog.

Finding Information

Creating a File object doesn't mean that an actual file exists with those characteristics. For instance, if I write this:

File f = new File("C:\AUTOEXEC.BAT");

The actual file may exist or not. You can test that by using the File exists() method. Here's a short application, FileFinder.java, that does just that:

The FileFinder application: displaying a file-not-found message.

Here's the code from FileFinder.java that checks to see if the file exists: 

File f = new File(tf.getText());

if (! f.exists())
{
  msg.setForeground(Color.red);
  msg.setText(tf.getText() + " does not exist");
}

Once you're sure that your File object points to a real file, you can use some of File methods to retreive more information about it. Here are some useful methods:

f.isDirectory();   // true if f is directory
f.isFile();        // true if f is a file
f.exits();         // true if file exists
f.length();        // number of bytes in file
f.getName();       // short file name
f.getAbsoluePath() // full name
f.list();          // all files in directory

The FileFinder application uses isDirectory() to tell if the file selected is a directory. If it is, then FileFinder uses getAbsolutePath() to print the directory:

The FileFinder application displaying a directory.
If the file is not a directory, then FileFinder retrieves the size of the file using the length() method:
The FileFinder application displaying information about a file.

This is the last section of this lesson.

 

Back to Top

 

Content Developed by Stephen Gilbert, Licensed under a Creative Commons License
Published by the Sofia Open Content Initiative
© 2004 Foothill-De Anza Community College District &The William and Flora Hewlett Foundation