Wednesday, 12 October 2016

Set Unique Identifier to File in Java

If you are developing an application that deals with files (or) directories, you may require to give unique identifier to each file to track rename, move operations  on particular file (or) directory. Java Files class provides setAttribute method, where you can set a property to file. In my previous post, I explained how to read the attributes associated with a file. Please go through my previous post, to get better understanding of file attributes.

Following is the complete working application, to set an unique identifier to file.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 * Utility class to perform various operations.
 * 
 * @author Hari Krishna
 *
 */
public class FileUtil {

 private static final String USER_ID_ATTRIBUTE = "user:objectid";
 private static final String USER_ID = "objectid";

 /**
  * Set Unique id to file.
  * 
  * @param path
  * @return Optional.empty() on failure condition, set an identifier to file.
  */
 public static Optional<String> setId(Path path) {
  String fileId = generateFileId();
  return setId(path, fileId);
 }

 /**
  * get the id associated with the file.
  * 
  * @param path
  * @return Optional.empty(), if no id associated with given file, else
  *         return the id.
  */
 public static Optional<String> getId(Path path) {
  UserDefinedFileAttributeView fileAttributeView = Files.getFileAttributeView(path,
    UserDefinedFileAttributeView.class);
  try {
   List<String> userAttributes = fileAttributeView.list();

   if (userAttributes.contains(USER_ID)) {
    byte[] b = (byte[]) Files.getAttribute(path, USER_ID_ATTRIBUTE);
    String objectId = new String(b, "UTF-8");
    return Optional.of(objectId);
   }
  } catch (IOException e) {
   return Optional.empty();
  }

  return Optional.empty();
 }

 public static Optional<String> setId(Path path, String fileId) {
  try {
   Files.setAttribute(path, USER_ID_ATTRIBUTE, fileId.getBytes("UTF-8"));
   return Optional.of(fileId);
  } catch (IOException e) {
   return Optional.empty();
  }
 }

 private static String generateFileId() {
  return UUID.randomUUID().toString();
 }
}

import java.io.File;
import java.nio.file.Path;
import java.util.Optional;

public class FileUtilTest {
 public static void main(String args[]) {
  Path path = new File("C:\\Users\\Documents\\Study\\javadoc.txt").toPath();

  Optional<String> fileIDOpt = FileUtil.setId(path);

  if (fileIDOpt.isPresent()) {
   System.out.println("New Id " +  fileIDOpt.get() + " is set to file");
  }

  fileIDOpt = FileUtil.getId(path);
  if (fileIDOpt.isPresent()) {
   System.out.println("Id associated with file is " + fileIDOpt.get());
  }
 }
}

2 comments:

  1. Very good information, thank you very much.

    ReplyDelete
  2. Very useful article thank you very much for the information!

    ReplyDelete