Page 661 - Introduction to Programming with Java: A Problem Solving Approach
P. 661

                A File object is not a file itself. It is just a container for information about a file. If you want to see whether a file with a particular name actually exists, you need to instantiate a file object with the filename you care about and then use that object to call File’s exists method, like this:
File paper = new File("daliLamaEssay.doc");
if (paper.exists())
{
}
System.out.println("Yes it exists.");
The paper object above represents a file in the current directory. The current directory is the direc- tory where the currently running program resides. To specify a file in a directory that’s different from the current directory, include the file’s path as part of the filename argument. A path is the location of a file within the computer’s directory structure. More specifically, a path is a series of one or more forward-slash- separated directory names that lead to a particular file. Optionally, you may use backslashes on Windows machines, but backslashes are messier than forward slashes because in Java a backslash is the escape char- acter, and you’d need to have two backslashes wherever you meant to have just one. So we recommend that you use forward slashes in your pathnames. There are two types of path—relative path and absolute path. A relative path goes from the current directory to the specified file. An absolute path goes from the root directory to the specified file. The root directory is the directory at the top of the computer’s directory struc- ture. An initial slash represents the root directory.
Suppose that a dalaiLamaEssay.doc file is in a re101 directory and the re101 directory is in the root directory. To instantiate a File object for the dalaiLamaEssay.doc file, use an absolute path
like this:
Apago PDF Enhancer
File paper = new File("/re101/daliLamaEssay.doc");
Suppose that a checkers.class file is in a checkers subdirectory of the current directory. To instantiate a File object for the checkers.class, use a relative path like this:
File paper = new File("checkers/checkers.class");
File Methods
Once you have a reference to a File object, you can use it to call any of the File class’s several useful methods. As indicated above, the boolean exists method returns true if the calling object’s file is present.Theboolean isfilemethodreturnstrueifthecallingobject’sfileisanormalfile.The boolean isDirectorymethodreturnstrueifthecallingobject’sfileisadirectory.(Adirectoryis consideredtobeafile,albeitaspecialkindoffile.)Theboolean deletemethoddeletesthecalling object’sfile.Theboolean mkdirmethodcreatesanewdirectoryandgivesitthenamespecifiedbythe argumentyougiveit.Theboolean renameTomethodchangesthenametothepathnamespecifiedby the argument you give it. The delete, mkdir, and renameTo methods return true if they succeed.
When you want to transfer data to and from files, it’s often helpful to see what files already exist, and it’s sometimes helpful to see how big they are. The program in Figure 15.13 displays this kind of information.
In the FileSizes program, note the “.” argument in the File constructor call. The dot is a special sym- bolthatrepresentsthecomputer’scurrentdirectory.Sonew File(".")instantiatesaFileobjectfor thecurrentdirectory.Similarly,new File("..")instantiatesaFileobjectfortheparentdirectory.
Note this statement in the FileSizes program:
File[] files = currentDirectory.listFiles();
15.9 The File Class 627















































































   659   660   661   662   663