Share this page 

Handle Windows file attribute Tag(s): IO


This example toogle the Archive DOS/Windows File Attribute of a given file. The Archive Attribute can be used to detect if a file is modified or not.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributes;

public class TestFileAttribute {
   public static void main(String[] args) throws IOException {
      String file = "c:/temp/howto.xml";
      Path pathFile = Paths.get(file);
      DosFileAttributes attr = Files.readAttributes(pathFile, DosFileAttributes.class);
      System.out.println(file + "  currrent archive attribute   = " + attr.isArchive());

      if (attr.isArchive()) {
          System.out.println("reset archive attributes");
          Files.setAttribute(pathFile, "dos:archive", false);
      }
      else {
         System.out.println("set archive attribute");
         Files.setAttribute(pathFile, "dos:archive", true);
      }
   }
}