- 获取用户定义的所有元数据
Path path = Paths.get("E:/1.sql"); UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class); try { for (String name : view.list()) { System.out.println(view.size(name) + " " + name); } } catch (Exception e) { e.printStackTrace(); }
- 自定义元数据
Path path = Paths.get("E:/1.sql"); UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class); try { view.write("file.description", Charset.defaultCharset().encode("This file contains private infomation!")); } catch (Exception e) { e.printStackTrace(); }
- 获取自定义元数据
Path path = Paths.get("E:/1.sql"); UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class); try { int size = view.size("file.description"); ByteBuffer buffer = ByteBuffer.allocateDirect(size); view.read("file.description", buffer); buffer.flip(); char[] array = Charset.defaultCharset().decode(buffer).array(); System.out.println(new String(array).toString()); } catch (Exception e) { e.printStackTrace(); }