Java Reference
In-Depth Information
The benefit of using the File.separator constant in your code is that Java will use the appropriate file separator
character in your file pathname depending on the operating system your program is executed.
If the pathname used to construct the File object is not absolute, the getAbsolutePath() method uses the
working directory to get the absolute path.
You have to deal with two “devils” when you work with I/O in Java. If you do not specify the absolute pathname,
your absolute path will be decided by the Java runtime and the operating system. If you specify the absolute
pathname, your code may not run on different operating systems. One way to handle this situation is to use a
configuration file, where you can specify a different file pathname for different operating systems, and you pass the
configuration file path to your program at startup.
The canonical path of a file is system-dependent and the call to the getCanonicalPath() may throw an
IOException . You must place this method call inside a try-catch block or throw an IOException from the method in
which you invoke this method. Some of the I/O method calls throw an IOException in situations when the requested
I/O operation fails.
Creating, Deleting, and Renaming Files
You can create a new file using the createNewFile() method of the File class:
// Create a File object to represent the abstract pathname
File dummyFile = new File("dummy.txt");
// Create the file
boolean fileCreated = dummyFile.createNewFile();
The createNewFile() method creates a new, empty file if the file with the specified name does not already exist.
It returns true if the file is created successfully; otherwise, it returns false. The method throws an IOException if an I/O
error occurs.
You can also create a temporary file in the default temporary file directory or a directory of your choice. To create
a temporary file in the default temporary directory, use the createTempFile() static method of the File class,
which accepts a prefix (at least three characters in length) and a suffix to generate the temporary file name.
File tempFile = File.createTempFile("kkk", ".txt");
You can use the mkdir() or mkdirs() method to create a new directory. The mkdir() method creates a directory
only if the parent directories specified in the pathname already exists. For example, if you want to create a new
directory called home in the users directory in the C: drive on Windows, you construct the File object representing
this pathname like so:
File newDir = new File("C:\\users\\home");
Now the newDir.mkdir() method will create the home directory only if the C:\users directory already exists.
However, the newDir.mkdirs() method will create the users directory if it does not exist in the C: drive, and hence, it
will create the home directory under the C:\users directory.
Deleting a file is easy. You need to use the delete() method of the File class to delete a file/directory. A directory
must be empty before you can delete it. The method returns true if the file/directory is deleted; otherwise, it returns
false. You can also delay the deletion of a file until the JVM terminates by using the deleteOnExit() method. This is
useful if you create temporary files in your program that you want to delete when your program exits.
// To delete the dummy.txt file immediately
File dummyFile = new File("dummy.txt");
dummyFile.delete();
 
Search WWH ::




Custom Search