Simulate touch command with Java

JavaFileTouch

Java Problem Overview


I want to change modification timestamp of a binary file. What is the best way for doing this?

Would opening and closing the file be a good option? (I require a solution where the modification of the timestamp will be changed on every platform and JVM).

Java Solutions


Solution 1 - Java

The File class has a setLastModified method. That is what ANT does.

Solution 2 - Java

My 2 cents, based on @Joe.M answer

public static void touch(File file) throws IOException{
    long timestamp = System.currentTimeMillis();
    touch(file, timestamp);
}

public static void touch(File file, long timestamp) throws IOException{
    if (!file.exists()) {
       new FileOutputStream(file).close();
    }
    
    file.setLastModified(timestamp);
}

Solution 3 - Java

Since File is a bad abstraction, it is better to use Files and Path:

public static void touch(final Path path) throws IOException {
    Objects.requireNonNull(path, "path is null");
    if (Files.exists(path)) {
        Files.setLastModifiedTime(path, FileTime.from(Instant.now()));
    } else {
        Files.createFile(path);
    }
}

Solution 4 - Java

Here's a simple snippet:

void touch(File file, long timestamp)
{
	try
	{
		if (!file.exists())
			new FileOutputStream(file).close();
		file.setLastModified(timestamp);
	}
	catch (IOException e)
	{
	}
}

Solution 5 - Java

I know Apache Ant has a Task which does just that.
See the source code of Touch (which can show you how they do it)

They use FILE_UTILS.setFileLastModified(file, modTime);, which uses ResourceUtils.setLastModified(new FileResource(file), time);, which uses a org.apache.tools.ant.types.resources.Touchable, implemented by org.apache.tools.ant.types.resources.FileResource...

Basically, it is a call to File.setLastModified(modTime).

Solution 6 - Java

This question only mentions updating the timestamp, but I thought I'd put this in here anyways. I was looking for touch like in Unix which will also create a file if it doesn't exist.

For anyone using Apache Commons, there's FileUtils.touch(File file) that does just that.

Here's the source from (inlined openInputStream(File f)):

public static void touch(final File file) throws IOException {
	if (file.exists()) {
		if (file.isDirectory()) {
			throw new IOException("File '" + file + "' exists but is a directory");
		}
		if (file.canWrite() == false) {
			throw new IOException("File '" + file + "' cannot be written to");
		}
	} else {
		final File parent = file.getParentFile();
		if (parent != null) {
			if (!parent.mkdirs() && !parent.isDirectory()) {
				throw new IOException("Directory '" + parent + "' could not be created");
			}
		}
		final OutputStream out = new FileOutputStream(file);
		IOUtils.closeQuietly(out);
	}
	final boolean success = file.setLastModified(System.currentTimeMillis());
	if (!success) {
		throw new IOException("Unable to set the last modification time for " + file);
	}
}

Solution 7 - Java

If you are already using Guava:

com.google.common.io.Files.touch(file)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsinuhepopView Question on Stackoverflow
Solution 1 - JavaYishaiView Answer on Stackoverflow
Solution 2 - JavaMmmh mmhView Answer on Stackoverflow
Solution 3 - JavasdgfsdhView Answer on Stackoverflow
Solution 4 - JavaZach-MView Answer on Stackoverflow
Solution 5 - JavaVonCView Answer on Stackoverflow
Solution 6 - JavaRaekyeView Answer on Stackoverflow
Solution 7 - JavaJoe23View Answer on Stackoverflow