menu

Thursday, November 20, 2014

Using Java NIO to Get File Attributes

Introduction:

     Today, I'll talk about how to get the attributes of a file like creation time, last access time and modify time using Java NIO library which comes with Java 7.

Enable Access Time Tracking In Win 7:

     If you're a Win 7 user, you will see that access time tracking is not enabled by default. You need to enable it by following command in cmd. If you need to disable again just change 0 to 1 in the command.

* sutil behavior set disablelastaccess 0 

Example 1:

     Here is the java code sample to get the creation, last access and modify time of a file. You need to create a text file named test.txt under your java project. We'll read this file using the System.getProperty("user.dir") which gives us the current working directory which is the directory of the project. We also use Calendar and SimpleDateFormat classes to set time and to print the result.

package file.attributes;

import java.io.File;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class FileAttributes {

 public static final String FILE_NAME = "test.txt";
 public static final SimpleDateFormat FORMATTER = new SimpleDateFormat("dd.MM.yyyy hh.mm.ss");

 public static void main(String[] args) {
    try {
File file = new File(System.getProperty("user.dir") + "/" + FILE_NAME);
FileSystem fileSystem = FileSystems.getDefault();
Path absolutePath = fileSystem.getPath(file.getAbsolutePath());
BasicFileAttributes fileAttrs = Files.readAttributes(absolutePath, BasicFileAttributes.class);
//Find and print creation time
FileTime creationTime = fileAttrs.creationTime();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(creationTime.toMillis());
System.out.println("Creation time : " + FORMATTER.format(cal.getTime()));
//Find and print last access time
FileTime lastAccessTime = fileAttrs.lastAccessTime();
cal.setTimeInMillis(lastAccessTime.toMillis());
System.out.println("Last access time : " + FORMATTER.format(cal.getTime()));
//Find and print last modify time
        FileTime lastModifiedTime = fileAttrs.lastModifiedTime();
cal.setTimeInMillis(lastModifiedTime.toMillis());
System.out.println("Last modified time : " + FORMATTER.format(cal.getTime()));
          } catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
  }
}
}

You will see the following results.

* Creation time : 18.11.2014 04.09.21
* Last access time : 20.11.2014 08.43.20
* Last modified time : 20.11.2014 08.36.37 

If you access or modify the file you will see the correspending properties changed.

* Creation time : 18.11.2014 04.09.21
* Last access time : 20.11.2014 08.50.50
* Last modified time : 20.11.2014 08.50.50

If you modify the file both access and modify times will change, on the other hand accessing a file will only change the access time.

Example 2:

     Here is another example that get the absolute path with a different way (using Paths.get(...) method) and change the last access time programmatically

package file.attributes;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class FileAttributes2 {

 public static final String FILE_NAME = "test.txt";
 public static final SimpleDateFormat FORMATTER = new SimpleDateFormat("dd.MM.yyyy hh.mm.ss");

 public static void main(String[] args) {
    try {
        // Set the last access time
Path absolutePath = Paths.get(System.getProperty("user.dir"), FILE_NAME);
long currTime = System.currentTimeMillis();
FileTime fileTime = FileTime.fromMillis(currTime);
Files.setAttribute(absolutePath, "basic:lastAccessTime", fileTime, java.nio.file.LinkOption.NOFOLLOW_LINKS);
        //Find and print last access time
BasicFileAttributes fileAttrs = Files.readAttributes(absolutePath, BasicFileAttributes.class);
FileTime lastAccessTime = fileAttrs.lastAccessTime();
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(lastAccessTime.toMillis());
System.out.println("Last access time : " + FORMATTER.format(cal.getTime()));
  } catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
  }
}
}

Conclusion:

     We saw how to use Java nio library to get the file attributes. You can get creation, last access and modify time of a file using nio. Although we enable tracking of last access time as explained in the first part of this paper, there is still a problem with the last access time. If you modify the file in the windows folder system it changes our program result, however accessing a file in the file system has no effect, while accessing it in the eclipse environment changes the last access time.


1 comment:

  1. But when one of many numbers does not turn up you lose all of the 20 bets you have have} made. What Einstein actually meant was that is not a|there is not any} mathematical trick that may help 1xbet you win at roulette. Each spin is an independent trial and, in lengthy run|the long term}, the casino will win. This is completely different to a game such as Blackjack where the possibilities change as playing cards are dealt. Although unlikely, if pink fails to come up 15 instances in a row, on the 16th guess, you must wager $32,768 in an attempt to come out $1 forward.

    ReplyDelete