10.8. Touching a File

Problem

You need to perform the equivalent of the Unix touch command; you want to create a file or update a file’s modified timestamp.

Solution

Use the touch() method from FileUtils. To use touch( ), pass it a File object; if the File does not exist, touch( ) will create a new file. If the file exists, the timestamp of the file will be updated to the current time. The following code demonstrates the touch( ) method on the file testFile.txt:

import org.apache.commons.io.FileUtils;

try {
    File testFile = new File( "testFile.txt" );

    // If testFile didn't already exists, create it
    // If testFile already exists, update the modified timestamp
    FileUtils.touch( testFile );  
} catch( IOException ioe ) {
    System.out.println( "Error touching testFile" );
}

If testFile.txt does not exist, the file will be created by the call to touch( ). If testFile.txt does exist, the last modified timestamp will be updated to the current time after the call to touch( ).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.142.2