开发者

Undeletable Folder in java

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.pri开发者_JAVA百科ntStackTrace();
                    }

    }


}

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


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


}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜