If you need to obtain information about a file, you should create an object of Java's File class. This class enables you to query the system about everything from the file's name to the time it was last modified. You can also use the File class to make new directories, as well as to delete and rename files. Create a File object by calling one of the class's three constructors, whose signatures are as follows : File(String path) File(String path, String name) File(File dir, String name) The first constructor creates a File object from the given full pathname (for example, C:\CLASSES\MYAPP.JAVA ). The second constructor creates the object from a separate path and a file. The third creates the object from a separate path and filename, with the path being associated with another File object. The File class features a full set of methods that gives your program a lot of file-handling options. Table 21.4 lists these methods along with their descriptions. The parameters are not shown here in this listing. In some cases, there is more than one version of a method with the same name. The difference is the number and type of arguments the each version expects. Check the JavaDocs for the exact arguments expected by the method. Table 21.4. Methods of the File Class
Note This is only successful when the Virtual Machine exits normally, not by abnormal exits. Troubleshooting Tip If you are having trouble with the File class. See "Understanding the Use of the File Class" in the "Troubleshooting" section at the end of the chapter. Creating Temporary FilesTemporary files are useful for a variety of purposes in many programs. To create a temporary file, you can use the static createTemporaryFile method in the File class. In Listing 21.5, a temporary file is created by passing in the name of the prefix and suffix of the file that you want to create. The string should contain the first several letters of the filename (minimum three characters ), followed by an extension. The VM will insert a four- or five-digit number that is guaranteed to be unique within this instance of the VM. If you do not provide an extension, it is automatically assigned the value of .tmp. You have to put the "." in the suffix for it to be added as an extension. Listing 21.5 Source Code for CreateTempFile.javaimport java.io.*; public class CreateTempFile { public static void main(String args[]) { try { File tempFile = File.createTempFile( "myfile", ".tmp" ); FileOutputStream fout = new FileOutputStream(tempFile); PrintStream out = new PrintStream(fout); out.println("Place this test string in the temp file"); } catch ( IOException ex ) { System.out.println("There was a problem creating/writing to the temp file"); ex.printStackTrace(); } } } After you run the CreateTempFile program in Listing 21.5, you should see a file called myfileXXXXX.tmp in your system's temp directory (where the X s are actually numbers ). There is also a constructor that will allow you to assign a directory location for the temporary file. |