Java Reference
In-Depth Information
The File class provides several methods for you to test the path that a File object encapsulates in various
ways, as well as the physical file or directory it may represent. You can determine whether or not an object
does represent a path to an existing file or directory, for example. If a File object does correspond to a real
file, you have methods available that you can use to modify the file in a number of ways.
Creating File Objects
You have a choice of four constructors for creating File objects. The simplest accepts a String object
as an argument that specifies a path for a file or a directory. For example, you could write the statement:
File myDir = new File("C:/j2sdk1.4.0/src/java/io");
This creates a File object encapsulating the path C:/j2sdk1.4.0/src/java/io . On my system,
this happens to be the path to the directory, io , containing the classes in the java.io package. On the
various flavors of the Microsoft Windows operating system, you can also use an escaped back slash
separator, \\ , when you define a path, instead of / , if you wish, but paths do tend to look rather busy if
you do. For instance:
File myDir = new File("C:\\jdk1.4\\src\\java\\io");
It also requires more typing.
Note that the File class constructor here does not check the string that you pass as the argument in any
way, so there is no guarantee that a File object encapsulates a valid representation of a path on your
system at all. For instance, this will compile and execute perfectly well:
File junk = new File("dwe\n:;?cc/.*\naa£%)("); // Not a valid path!
The argument to the constructor here does not define a valid file or directory path - at least not under
any operating system that I am familiar with - but that statement compiles and executes. We can
therefore deduce that you can pass any string as an argument to the File class constructor. It is up to
you to ensure that it is valid for your system.
To specify a pathname to a file, you just need to make sure that the string that you pass as an argument
to the constructor does refer to a file, and not a directory. For example, the statement:
File myFile = new File("C:/j2sdk1.4.0/src/java/io/File.java");
sets the object, myFile , to correspond to the source file for the definition of the class, File .
An important and easily overlooked characteristic of File objects is that they are immutable . Once you
have created a File object you cannot change the path it encapsulates. We will see later that you can
change the name of the physical file it references by using the rename() method belonging to the
File object, but this will not change the File object itself. The File object will still encapsulate the
original path so that once the file name has been changed the path encapsulated by the File object will
no longer refer to an existing file. This can be confusing if you don't realize this is the case. Using this
File object to test whether the file exists, for instance, will return false .
Search WWH ::




Custom Search