Java Files  «Prev  Next»


Lesson 3The File class
Objective Examine how to create File Objects

History of Java I/O Fundamentals

IO (input/output) has been around since the beginning of Java and one could read and write files along with some other common operations. Then with Java 1.4, Java added more I/O functionality and cleverly named it NIO which stands for "new I/O." You will not be asked about those Java 1.4 additions on the exam. The APIs prior to Java 7 still had a few limitations when you had to write applications that focused heavily on files and file manipulation. Trying to write a small routine to list all the files created during the past day within a directory tree was challenging. There was no support for navigating directory trees, and just reading attributes of a file was also quite hard. In Java 7, this whole routine is approximately 17 lines of code.
Since the Java 7 functionality was added to package names that begin with java.nio, the new name was NIO.2. Since NIO 2 builds upon the original I/O, some of those concepts are still tested on the exam in addition to the new parts. Fortunately, you will not have to become a total I/O or NIO expert to do well on the exam. The intention of this site preparation was to include just the basic aspects of these technologies, and in this module, we cover the essentials to get through these objectives on the exam.
Java File Object Class

Java File Class

The java.io.File class represents a filename on the host system. This filename may also include an absolute or relative path component. The File class also attempts to abstract system-dependent filename features. For example, using the
File.separator
class variable, you can get a file's path separator character.
A File object does not represent a file. Occasionally this distinction is crucial. For instance, File objects can represent directories as well as files. Also, you cannot assume that a file exists just because you have a File object for a file.
There are three constructor methods in java.io.File. Each takes some variation of a filename as an argument(s).

Three constructor methods in java.io.File

  1. The simplest File constructor has the path argument as a string with either a full or relative pathname to the file that can be understood by the host operating system.
    public File(String path)
    

  2. If you like, you can separate the path and filename using another version of the constructor.
    public File(String path, String name)
    

  3. Finally there is a constructor that is like the previous constructor but that uses a File object for the path instead of a string. Here name is the filename and dir is the name of the directory that contains the file. In this case, dir is a File object itself instead of a string.
    public File(File dir, String name)
Instances of the java.io.File class represent filenames on the local system, not actual files. Occasionally, this distinction is crucial. For instance, File objects can represent directories as well as files. Also, you cannot assume that a file exists just because you have a File object for a file.
public class File extends Object implements Serializable

In Java 2, the File class also implements the java.lang.Comparable interface:
public class File extends Object implements Serializable, Comparable // Java 2