You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何批量获取与设置Windows文件的所有属性?

解决方案:Windows文件属性批量备份与恢复

1. 批量获取/设置Windows文件属性的方法

Java NIO提供了DosFileAttributes和DosFileAttributeView接口,专门用于处理Windows系统的DOS文件属性,支持一次性读取所有属性并批量设置,无需逐个调用getAttribute()/setAttribute()。

读取源文件所有DOS属性

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.DosFileAttributes;

// 读取源文件的所有DOS属性
Path sourcePath = Path.of("C:\\path\\to\\source\\desktop.ini");
DosFileAttributes sourceAttrs = Files.readAttributes(sourcePath, DosFileAttributes.class);

将属性批量设置到目标文件

通过DosFileAttributeView可以直接设置所有DOS属性,包括系统隐藏属性:

import java.nio.file.attribute.DosFileAttributeView;

// 获取目标文件的DOS属性视图
Path targetPath = Path.of("C:\\path\\to\\backup\\desktop.ini");
DosFileAttributeView targetView = Files.getFileAttributeView(targetPath, DosFileAttributeView.class);

// 批量设置所有属性
targetView.setHidden(sourceAttrs.isHidden());
targetView.setSystem(sourceAttrs.isSystem()); // 关键:设置系统属性
targetView.setReadOnly(sourceAttrs.isReadOnly());
targetView.setArchive(sourceAttrs.isArchive());
targetView.setOffline(sourceAttrs.isOffline());
targetView.setNotContentIndexed(sourceAttrs.isNotContentIndexed());
targetView.setSparse(sourceAttrs.isSparse());

这个方法可以完美解决desktop.ini这类系统隐藏文件的属性恢复问题。

2. Windows系统下所有DOS文件属性列表

对应Java DosFileAttributes接口的属性,完整列表如下:

  • hidden:普通隐藏文件标记
  • system:系统文件标记(如desktop.ini、ntldr等)
  • readonly:只读文件标记
  • archive:归档标记(通常备份工具用来识别已修改需要备份的文件)
  • offline:文件内容脱机存储标记
  • notContentIndexed:禁止系统内容索引服务索引该文件
  • sparse:稀疏文件标记(用于存储大但实际占用空间小的文件)
  • reparsePoint:重解析点标记(如符号链接、 junction 点)

3. 可序列化属性快照类的实现建议

如果需要通过Socket传输属性信息,你可以封装一个实现Serializable接口的快照类,方便网络传输和后续恢复:

import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.DosFileAttributes;
import java.nio.file.attribute.DosFileAttributeView;
import java.io.IOException;

public class WinFileAttrsSnapshot implements Serializable {
    private boolean hidden;
    private boolean system;
    private boolean readOnly;
    private boolean archive;
    private boolean offline;
    private boolean notContentIndexed;
    private boolean sparse;

    // 从源文件属性构建快照
    public WinFileAttrsSnapshot(DosFileAttributes attrs) {
        this.hidden = attrs.isHidden();
        this.system = attrs.isSystem();
        this.readOnly = attrs.isReadOnly();
        this.archive = attrs.isArchive();
        this.offline = attrs.isOffline();
        this.notContentIndexed = attrs.isNotContentIndexed();
        this.sparse = attrs.isSparse();
    }

    // 从文件路径直接创建快照
    public WinFileAttrsSnapshot(Path filePath) throws IOException {
        this(Files.readAttributes(filePath, DosFileAttributes.class));
    }

    // 将快照属性应用到目标文件
    public void applyTo(Path targetPath) throws IOException {
        DosFileAttributeView view = Files.getFileAttributeView(targetPath, DosFileAttributeView.class);
        view.setHidden(hidden);
        view.setSystem(system);
        view.setReadOnly(readOnly);
        view.setArchive(archive);
        view.setOffline(offline);
        view.setNotContentIndexed(notContentIndexed);
        view.setSparse(sparse);
    }

    // 可选:添加getter方法用于调试或扩展
    public boolean isSystem() {
        return system;
    }
}

使用方式:

  1. 备份端:创建WinFileAttrsSnapshot对象,通过Socket发送
  2. 恢复端:接收对象后调用applyTo(targetPath)完成属性设置

内容的提问来源于stack exchange,提问作者Devorious

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 21:53:12