使用 Java 在 Windows 上隐藏文件/文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1294989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:54:29  来源:igfitidea点击:

Make a File/Folder Hidden on Windows with Java

javafiledirectoryhidden

提问by Kryten

I need to make files and folders hidden on both Windows and Linux. I know that appending a '.' to the front of a file or folder will make it hidden on Linux. How do I make a file or folder hidden on Windows?

我需要在 Windows 和 Linux 上隐藏文件和文件夹。我知道附加一个“。” 到文件或文件夹的前面将使其在 Linux 上隐藏。如何在 Windows 上隐藏文件或文件夹?

采纳答案by Andrew

For Java 6 and below,

对于 Java 6 及以下版本,

You will need to use a native call, here is one way for windows

您将需要使用本机调用,这是 Windows 的一种方法

Runtime.getRuntime().exec("attrib +H myHiddenFile.java");

You should learn a bit about win32-api or Java Native.

您应该了解一些有关 win32-api 或 Java Native 的知识。

回答by Marian

The functionality that you desire is a feature of NIO.2 in the upcoming Java 7.

您想要的功能是即将推出的 Java 7 中 NIO.2 的一个特性。

Here's an article describing how will it be used for what you need: Managing Metadata (File and File Store Attributes). There's an example with DOS File Attributes:

这是一篇描述如何将其用于您需要的文章:管理元数据(文件和文件存储属性)。有一个DOS 文件属性的例子:

Path file = ...;
try {
    DosFileAttributes attr = Attributes.readDosFileAttributes(file);
    System.out.println("isReadOnly is " + attr.isReadOnly());
    System.out.println("isHidden is " + attr.isHidden());
    System.out.println("isArchive is " + attr.isArchive());
    System.out.println("isSystem is " + attr.isSystem());
} catch (IOException x) {
    System.err.println("DOS file attributes not supported:" + x);
}

Setting attributes can be done using DosFileAttributeView

可以使用DosFileAttributeView设置属性

Considering these facts, I doubt that there's a standard and elegant way to accomplish that in Java 6 or Java 5.

考虑到这些事实,我怀疑在 Java 6 或 Java 5 中是否有一种标准而优雅的方式来实现这一点。

回答by Marian

this is what I use:

这是我使用的:

void hide(File src) throws InterruptedException, IOException {
    // win32 command line variant
    Process p = Runtime.getRuntime().exec("attrib +h " + src.getPath());
    p.waitFor(); // p.waitFor() important, so that the file really appears as hidden immediately after function exit.
}

回答by Steve Emmerson

Java 7 can hide a DOS file this way:

Java 7 可以通过以下方式隐藏 DOS 文件:

Path path = ...;
Boolean hidden = path.getAttribute("dos:hidden", LinkOption.NOFOLLOW_LINKS);
if (hidden != null && !hidden) {
    path.setAttribute("dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
}

Earlier Java-s can't.

早期的 Java-s 不能。

The above code will not throw an exception on non-DOS file-systems. If the name of the file starts with a period, then it will also be hidden on UNIX file-systems.

上面的代码不会在非 DOS 文件系统上抛出异常。如果文件名以句点开头,那么它也会在 UNIX 文件系统上隐藏。

回答by Vighanesh Gursale

String cmd1[] = {"attrib","+h",file/folder path};
Runtime.getRuntime().exec(cmd1);

Use this code it might solve you problem

使用此代码可能会解决您的问题

回答by redlasha

on windows, using java nio, Files

在 Windows 上,使用 java nio,文件

Path path = Paths.get(..); //< input target path
Files.write(path, data_byte, StandardOpenOption.CREATE_NEW); //< if file not exist, create 
Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS); //< set hidden attribute

回答by Mark Burleigh

Here is a fully compilable Java 7 code sample which hides an arbitrary file on Windows.

这是一个完全可编译的 Java 7 代码示例,它隐藏了 Windows 上的任意文件。

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


class A { 
    public static void main(String[] args) throws Exception
    { 
       //locate the full path to the file e.g. c:\a\b\Log.txt
       Path p = Paths.get("c:\a\b\Log.txt");

       //link file to DosFileAttributes
       DosFileAttributes dos = Files.readAttributes(p, DosFileAttributes.class);

       //hide the Log file
       Files.setAttribute(p, "dos:hidden", true);

       System.out.println(dos.isHidden());

    }
 } 

To check the file is hidden. Right-click on the file in question and you will see after the execution of the court that the file in question is truly hidden.

检查文件是否隐藏。右键单击相关文件,您将在执行法院后看到相关文件确实被隐藏了。

enter image description here

在此处输入图片说明