日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

javase获取项目根目录_JavaSE:如何设置/获取您自己的文件和目录属性

發(fā)布時間:2023/12/3 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javase获取项目根目录_JavaSE:如何设置/获取您自己的文件和目录属性 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

javase獲取項目根目錄

在上一篇文章“確定特定文件系統(tǒng)支持的視圖”中 ,了解如何詢問文件系統(tǒng)存儲,并了解特定文件屬性視圖的可支持性。

簡而言之,我們將探索最高級和最重要的文件屬性視圖之一,即用戶定義的文件屬性視圖 。

特別是,在系統(tǒng)之間進行集成期間,我在交換文件中經常使用此功能,以便從用戶和文件內容中隱藏文件元數(shù)據(jù)和與安全性有關的信息。 因此,文件內容將僅與文件的內容有關,而不再是無關的元數(shù)據(jù) 。

因此,如果發(fā)現(xiàn)沒有足夠的內置屬性來滿足需要,或者如果您要與文件關聯(lián)某些唯一的元數(shù)據(jù)( 對文件系統(tǒng)有意義 ),則可以定義自己的屬性。

NIO.2提供了用戶定義的文件屬性視圖,并通過UserDefinedFileAttributeView接口擴展了屬性。 利用此功能,您可以將對您的用例有用的任何屬性關聯(lián)到文件。

在這里,您應該知道如何:

  • 檢查用戶定義的屬性可支持性
  • 用戶定義屬性的操作如下:
  • 定義用戶屬性。
  • 列出用戶定義的屬性名稱和值大小。
  • 獲取用戶定義的屬性的值。
  • 刪除文件的用戶定義屬性。
  • 這是先前定義的操作的類,您還需要使用JDK 7+:

    import static java.lang.System.err; import static java.lang.System.out; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import static java.nio.file.Files.getFileStore; import java.nio.file.Path; import static java.nio.file.Paths.get; import java.nio.file.attribute.UserDefinedFileAttributeView;/*** GET/SET FILES METADATA THROUGH THE NEW JAVA.NIO.FILE.ATTRIBUTE API.** @author mohamed_taman** @see java.nio.file.attribute* @see java.nio.file.Files*/ public class MetadataOperations {private static FileSystem fs = FileSystems.getDefault();private static Path path = get("C:", "workspace/NIO2", "resources", "TOC.txt");public static void main(String... args) {//User-Defined File Attributes View |userDefinedViewsOperations();}private static void userDefinedViewsOperations() {try {// Check User-Defined Attributes Supportabilityif (getFileStore(path).supportsFileAttributeView(UserDefinedFileAttributeView.class)) {// 1- Define a User Attribute.UserDefinedFileAttributeView udfav = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);out.println("Attrs. before deletion. its size: " + udfav.list().size());for (String name : udfav.list()) {out.println(udfav.size(name) + " " + name);}int written = udfav.write("file.description", Charset.defaultCharset().encode("This file contains private information about HOL2846!"));// 2- List User-Defined Attribute Names and Value Sizes.for (String name : udfav.list()) {out.println(udfav.size(name) + " " + name);}// 3- Get the Value of a User-Defined Attribute.int size = udfav.size("file.description");ByteBuffer bb = ByteBuffer.allocateDirect(size);udfav.read("file.description", bb);bb.flip();out.println(Charset.defaultCharset().decode(bb).toString());/*** Note: Using the UserDefinedFileAttributeView.size() method, * you can easily set the correct size of the buffer that represents * the value of the user-defined attribute.* * Note: You can also read an attribute by using the getAttribute() method. * The value is returned as byte array (byte[]).* */// 4- Delete a File’s User-Defined Attribute.out.println("Attrs. before deletion.");for (String name : udfav.list()) {out.println(udfav.size(name) + " " + name);}udfav.delete("file.description");out.println("Attrs. after deletion.");for (String name : udfav.list()) {out.println(udfav.size(name) + " " + name);}} else {out.println(path.toAbsolutePath().toString() + ", Doesn't support user defined attributes.");}} catch (Exception e) {err.println(e);}} }

    資源資源

    • JavaSE 7,8:確定特定文件系統(tǒng)支持的視圖
    • JSR 203:針對JavaTM平臺(“ NIO.2”)的更多新I / O API
    • Java SE教程:文件I / O(具有NIO.2功能)

    翻譯自: https://www.javacodegeeks.com/2014/03/javase-how-to-setget-your-own-files-and-directory-attributes.html

    javase獲取項目根目錄

    總結

    以上是生活随笔為你收集整理的javase获取项目根目录_JavaSE:如何设置/获取您自己的文件和目录属性的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。