Get the Value of a User-Defined Attribute - Java File Path IO

Java examples for File Path IO:File Attribute

Description

Get the Value of a User-Defined Attribute

Demo Code

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserDefinedFileAttributeView;

public class Main {
  public static void main(String[] args) {
    Path path = Paths.get("C:/folder1/folder2/folder4", "test.txt");
    UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path,
        UserDefinedFileAttributeView.class);

    try {/*from w  w  w  . j  a v a  2  s .com*/
      int size = udfav.size("file.description");
      ByteBuffer bb = ByteBuffer.allocateDirect(size);
      udfav.read("file.description", bb);
      bb.flip();
      System.out.println(Charset.defaultCharset().decode(bb).toString());
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Result


Related Tutorials