Touching a File in Java

The following is a tutorial on how to create a empty file on the filesystem:

// Destination directory
File destinationDir = new File(“/some/path”);

// The ‘touched’ file
File doneFile = new File(destinationDir, “some_file_name”);

// The JVM will only ‘touch’ the file if you instantiate a
// FileOutputStream instance for the file in question.
// You don’t actually write any data to the file through
// the FileOutputStream.  Just instantiate it and close it.
FileOutputStream doneFOS = null;
try {
    doneFOS = new FileOutputStream(doneFile);
} catch (FileNotFoundException e) {
    // Handle error
}

try {
    doneFOS.close();
} catch (IOException e) {
    // Handle error
}

Leave a Reply