Answers for "add file attrributes java"

0

add file attrributes java

Path file = Paths.get(Example.class.getResource("/samples/example.txt").toURI()).toAbsolutePath();

UserDefinedFileAttributeView view = Files.getFileAttributeView(file, UserDefinedFileAttributeView.class);

/* The file attribute */
String name = "com.javacreed.attr.1";
String value = "Custom Value 1";

/* Write the properties */
byte[] bytes = value.getBytes("UTF-8");
ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);
writeBuffer.put(bytes);
writeBuffer.flip();
view.write(name, writeBuffer);

/* Read the property */
final ByteBuffer readBuffer = ByteBuffer.allocate(view.size(name));
view.read(name, readBuffer);
readBuffer.flip();
final String valueFromAttributes = new String(readBuffer.array(), "UTF-8");
System.out.println("File Attribute: " + valueFromAttributes);
Posted by: Guest on June-21-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language