gpt4 book ai didi

java - 通过java取消隐藏文本文件

转载 作者:行者123 更新时间:2023-12-01 16:28:26 24 4
gpt4 key购买 nike

我尝试隐藏和取消隐藏文本文件。我只能隐藏,但当我尝试取消隐藏时,我收到一条错误消息。

        try {
//Hide file;
Process process = Runtime.getRuntime().exec("cmd.exe /c attrib +h test.txt");
//wait for process to get over (i.e. for file hiding)
process.waitFor();
} catch (Exception e) {
e.getMessage();
}

//Now, let's test whether file has been hidden or not
boolean fileHidden = fileToBeHidden.isHidden();

if (fileHidden) {
System.out.println(fileName + " is hidden ");
} else {
System.out.println(fileName + " isn't hidden ");
}

此方法正确隐藏文件,但我无法再次取消隐藏它

最佳答案

尝试此方法来隐藏和取消隐藏文件。

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Test {

public static void main(String[] args) {
Test.fileProcess("test.txt", true);
Test.fileProcess("test.txt", false);
}

public static void fileProcess(String fileName, boolean hide) {
try {

// you can change your full
String filePath = System.getProperty("user.dir")
+ File.separator
+ "files"
+ File.separator + fileName;

File f = new File(filePath);
if(!f.exists() && !f.isDirectory()) {
System.out.println(fileName + " file is not exist ");
return;
}

Path file = Paths.get(filePath);
Files.setAttribute(file, "dos:hidden", hide);

f = new File(filePath);

boolean fileHidden = f.isHidden();
if (fileHidden) {
System.out.println(fileName + " is hidden ");
} else {
System.out.println(fileName + " isn't hidden ");
}

} catch (Exception e) {
e.printStackTrace();
}
}

}

输出如下:

test.txt is hidden 
test.txt isn't hidden

关于java - 通过java取消隐藏文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62112966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com