Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

The touch command in Linux is a handy way to change the access time and modification time of a file or directory. It can also be used to create an empty file quickly.

In this short tutorial, we'll see how to simulate this command in Java.

2. Use Plain Java

2.1. Create Our touch Method

Let's create our touch method in Java. This method will create an empty file if the file doesn't exist. It can change the access time or modification time of the file, or both.

Moreover, it can also use the custom time passed in from input:

public static void touch(String path, String... args) throws IOException, ParseException {
    File file = new File(path);
    if (!file.exists()) {
        file.createNewFile();
        if (args.length == 0) {
            return;
        }
    }
    long timeMillis = args.length < 2 ? System.currentTimeMillis() : new SimpleDateFormat("dd-MM-yyyy hh:mm:ss").parse(args[1]).getTime();
    if (args.length > 0) {
        // change access time only
        if ("a".equals(args[0])) {
            FileTime accessFileTime = FileTime.fromMillis(timeMillis);
            Files.setAttribute(file.toPath(), "lastAccessTime", accessFileTime);
            return;
        }
        // change modification time only
        if ("m".equals(args[0])) {
            file.setLastModified(timeMillis);
            return;
        }
    }
    // other inputs will change both
    FileTime accessFileTime = FileTime.fromMillis(timeMillis);
    Files.setAttribute(file.toPath(), "lastAccessTime", accessFileTime);
    file.setLastModified(timeMillis);
}

As can be seen above, our method uses varargs to avoid overloading, and we can pass a custom time into this method in “dd-MM-yyyy hh:mm:ss” format.

2.2. Use Our touch Method

Let's create an empty file with our method:

touch("test.txt");

And use the stat command in Linux to view file information:

stat test.txt

We can see the access and modification times of the file in the stat output:

Access: 2021-12-07 10:42:16.474007513 +0700
Modify: 2021-12-07 10:42:16.474007513 +0700

Now, let's change its access time with our method:

touch("test.txt", "a", "16-09-2020 08:00:00");

Then we'll get this file information with the stat command again:

Access: 2020-09-16 08:00:00.000000000 +0700
Modify: 2021-12-07 10:42:16.474007000 +0700

3. Use Apache Commons Lang

We can also use the FileUtils class from the Apache Commons Lang library. This class has an easy-to-use touch() method, which will also create an empty file if the file doesn't exist yet:

FileUtils.touch(new File("/home/baeldung/test.txt"));

Note that if the file already exists, this method will only update the modification time of the file, not the access time.

4. Conclusion

In this article, we’ve seen how to simulate the Linux touch command in Java.

As always, the example code from this article can be found over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments