简体   繁体   English

Java中的不可删除文件夹

[英]Undeletable Folder in java

I tried to create a Undeletable folder using java code. 我试图使用Java代码创建一个Undeletable文件夹 I use the command "cacls (Foldername) /e /c /d %username%" in command prompt it worked fine.Then i tried to implement in my java class (Eclipse IDE). 我在命令提示符下使用命令“ cacls(文件夹名称)/ e / c / d%username%” ,它工作正常。然后,我尝试在我的Java类(Eclipse IDE)中实现。 It doesn't work. 没用

UndeletableFolder.java 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();
                    }

    }


}

if any other way to do this. 如果有其他方法可以做到这一点。 Thanks in advance. 提前致谢。

Is the current working directory set to the parent directory of your "hidden" directory when you invoke the command? 调用命令时,当前工作目录是否设置为“隐藏”目录的父目录? You can change it with ProcessBuilder.directory(java.io.File) . 您可以使用ProcessBuilder.directory(java.io.File)进行更改。

Try this code it works fine.It suits for both file and folder. 尝试使用此代码可以正常工作,它适合文件和文件夹。

public class Undeletable {

    public static void main(String[] args) {

        Runtime runtime=Runtime.getRuntime();
        try {
            Process process= runtime.exec("cmd.exe /c  start cacls text.txt /e /d %username%");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM