Close

Java IO & NIO - Files.setAttribute() Examples

Java IO & NIO Java Java API 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Method:

public static Path setAttribute(Path path,
                                String attribute,
                                Object value,
                                LinkOption... options)
                         throws IOException

Sets the value of a file attribute.

Parameters:
path - the path to the file
attribute - the attribute to set
value - the attribute value
options - options indicating how symbolic links are handled
Returns:
the path parameter


Examples


package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.util.Map;

public class SetAttributeExample {

public static void main(String... args) throws IOException {
Path path = Files.createTempFile("test-file", ".txt");

System.out.println("-- creationTime before --");
Map<String, Object> attributes = Files.readAttributes(path, "creationTime");
System.out.println(attributes);

Files.setAttribute(path, "creationTime",
FileTime.from(Instant.now()
.minusSeconds(10000 * 24 * 60 * 60)));

System.out.println("-- creationTime after --");
attributes = Files.readAttributes(path, "creationTime");
System.out.println(attributes);
}
}

Output

-- creationTime before --
{creationTime=2017-12-17T18:10:39.037574Z}
-- creationTime after --
{creationTime=1990-08-01T18:10:39.042576Z}




See Also