You can use the File class (see Tips: “Constructing a File Class” and “Manipulating File Names With the File Class”) to perform operations on files that reside on your operating system’s file system. These operations include creating directories, obtaining information about the file, renaming the file, deleting the file, and so on. The File class provides some of the following methods:
public boolean exists() // Answers "Does this file exist?"public boolean canWrite() // Answers "Can the program write to this file?"public boolean canWrite() // Answers "Can the program read from this file?"public boolean mkdir() // Create a directory for this filenamepublic boolean delete() // Delete a directory/file with this filenamepublic boolean renameTo(File file) // Renames or moves the file/dir
The following code illustrates how to use these methods. Assume that your system has a file “myfile.xyz” under the directory “C:fooaraz”. Also assume that this code is running in the directory “C:fooaraz”.
1. File myFile = null;2. 3. myFile = new File("C:\foo\bar\baz\myfile.xyz\myfile.xyz");4. System.out.println("1::myFile exists = " + myFile.exists());5. System.out.println("2::can write = " + myFile.canWrite());6. System.out.println("3::can read = " + myFile.canRead());7. 8. File myDir = new File(