javawindowsaclcacls

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

    }


}

if any other way to do this. Thanks in advance.


Solution

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