Saturday, 7 January 2023

Touch the file in Java (Update the file timestamp)

If you are from Linux background, you might have seen ‘touch’ command, which sets the modification and access times of files. 

 

Touch command address following both scenarios

Scenario 1: Update the timestamp when the file exits.

Scenario 2: Create an empty file when the file is not exists.

 

We can achieve the same kind of behavior using ‘Files.setLastModifiedTime’ method. ‘Files.setLastModifiedTime’ method update the file's last modified time attribute.

 

Example

Files.setLastModifiedTime(filePath, FileTime.from(Instant.now()));

 


Let’s experiment it with below example.

$ls -lart
total 0
drwxrwxrwt  16 root     wheel  512 Jan  7 19:03 ..
-rw-r--r--   1 krishna  wheel    0 Jan  7 19:03 a.txt
-rw-r--r--   1 krishna  wheel    0 Jan  7 19:03 b.txt
-rw-r--r--   1 krishna  wheel    0 Jan  7 19:03 c.txt
drwxr-xr-x   5 krishna  wheel  160 Jan  7 19:03 .

 

As you see above example, files a.txt, b.txt, c.txt are updated 19:03. Let’s run below application and confirm the touch functionality.

 

package com.sample.app.files;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.util.Objects;

public class FileTouchDemo {

	public static void touch(String filePath) throws IOException {
		Objects.requireNonNull(filePath, "filePath is null");
		touch(Paths.get(filePath));
	}

	public static void touch(final File filePath) throws IOException {
		Objects.requireNonNull(filePath, "filePath is null");
		touch(filePath.toPath());
	}

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

	public static void main(String[] args) throws IOException {
		final String filePath1 = "/Users/Shared/filePrograms/a.txt";
		final String filePath2 = "/Users/Shared/filePrograms/b.txt";
		final String filePath3 = "/Users/Shared/filePrograms/c.txt";
		final String filePath4 = "/Users/Shared/filePrograms/d.txt";

		touch(filePath1);
		touch(new File(filePath2));
		touch(Paths.get(filePath3));
		touch(Paths.get(filePath4));

		System.out.println("Files last modification time is updated");
	}

}

 

Output from Linux terminal.

$ls -lart
total 0
drwxrwxrwt  16 root     wheel  512 Jan  7 19:03 ..
-rw-r--r--   1 krishna  wheel    0 Jan  7 19:23 a.txt
-rw-r--r--   1 krishna  wheel    0 Jan  7 19:23 c.txt
-rw-r--r--   1 krishna  wheel    0 Jan  7 19:23 b.txt
-rw-r--r--   1 krishna  wheel    0 Jan  7 19:23 d.txt
drwxr-xr-x   6 krishna  wheel  192 Jan  7 19:23 .

From the output, you can confirm d.txt is created as it wasn't exists already and last modified time of a.txt, b.txt and c.txt is updated to current time.

 

You may like

file and stream programs in Java

How to process a huge file line by line in Java?

How to get the directory size in Java?

Convert string to InputStream in Java

Read the data from BufferedReader in Java

Read file content using BufferedReader in Java

No comments:

Post a Comment