Undeletable Folder in java

 ̄綄美尐妖づ 提交于 2019-12-02 20:31:52

问题


I tried to create a Undeletable folder using java code. 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). It doesn't work.

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.


回答1:


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).




回答2:


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


}


来源:https://stackoverflow.com/questions/7282901/undeletable-folder-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!