JavaQuick Tip: How To Update Last Modified Time in Java

Quick Tip: How To Update Last Modified Time in Java

Java Developer Tutorials

There are times that a developer may need to change the last modified time for a file or folder. This Java programming tutorial teaches you how to perform this task. Here a simple code example showing how to modify the last modified property of files and folders in Java:

*/

import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import java.io.*;

public class UpdateLastModifiedTime
{
	public static void main(String args[])
	{
		UpdateLastModifiedTime updateLastModifiedTime = new UpdateLastModifiedTime();
		updateLastModifiedTime.proceed();
	}
	
	private void proceed()
	{
		try
		{
			Path currDir = Paths.get("."); //Current directory
			FileTime now = FileTime.fromMillis(System.currentTimeMillis());
			Files.setLastModifiedTime(currDir, now);
			System.out.println("Updated the last modified time for " + currDir);
		}catch(IOException ioe)
		{
			System.out.println("Exception: " + ioe);
		}
	}
}

/*

Running this code snippet results in the following output:

[root@mypc]# java UpdateLastModifiedTime
Updated the last modified time for .

Read more Java programming tutorials.

Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories