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

javase获取项目根目录

在上一篇文章“确定特定文件系统支持的视图”中 ,了解如何询问文件系统存储,并了解特定文件属性视图的可支持性。

简而言之,我们将探索最高级和最重要的文件属性视图之一,即用户定义的文件属性视图

特别是,在系统之间进行集成期间,我在交换文件中经常使用此功能,以便从用户和文件内容中隐藏文件元数据和与安全性有关的信息。 因此,文件内容将仅与文件的内容有关,而不再是无关的元数据

因此,如果发现没有足够的内置属性来满足需要,或者如果您要与文件关联某些唯一的元数据( 对文件系统有意义 ),则可以定义自己的属性。

NIO.2提供了用户定义的文件属性视图,并通过UserDefinedFileAttributeView接口扩展了属性。 利用此功能,您可以将对您的用例有用的任何属性关联到文件。

在这里,您应该知道如何:

  1. 检查用户定义的属性可支持性
  2. 用户定义属性的操作如下:
    1. 定义用户属性。
    2. 列出用户定义的属性名称和值大小。
    3. 获取用户定义的属性的值。
    4. 删除文件的用户定义属性。

这是先前定义的操作的类,您还需要使用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 Supportability
 
            if (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);
        }
    }
}

资源资源

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

javase获取项目根目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值