我尝试使用 java 代码创建一个不可删除的文件夹。我在命令提示符中使用命令“cacls (Foldername) /e /c /d %username%”它工作正常。然后我尝试在我的 java 类(Eclipse IDE)中实现。它不起作用。
UndeletableFolder.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class UndeletableFolder {
public static void main(String args[]){
Runtime rt = Runtime.getRuntime();
String cmd=("cacls hidden /e /c /d %username%");
ProcessBuilder p = new ProcessBuilder(new String[] { "cmd.exe", "/C",
cmd });
Process pro;
try {
pro = p.start();
InputStream is = pro.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如果有其他方法可以做到这一点。提前致谢。