add file attrributes java

Solutions on MaxInterview for add file attrributes java by the best coders in the world

showing results for - "add file attrributes java"
Julian
08 Jan 2017
1Path file = Paths.get(Example.class.getResource("/samples/example.txt").toURI()).toAbsolutePath();
2
3UserDefinedFileAttributeView view = Files.getFileAttributeView(file, UserDefinedFileAttributeView.class);
4
5/* The file attribute */
6String name = "com.javacreed.attr.1";
7String value = "Custom Value 1";
8
9/* Write the properties */
10byte[] bytes = value.getBytes("UTF-8");
11ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);
12writeBuffer.put(bytes);
13writeBuffer.flip();
14view.write(name, writeBuffer);
15
16/* Read the property */
17final ByteBuffer readBuffer = ByteBuffer.allocate(view.size(name));
18view.read(name, readBuffer);
19readBuffer.flip();
20final String valueFromAttributes = new String(readBuffer.array(), "UTF-8");
21System.out.println("File Attribute: " + valueFromAttributes);