0

I'm trying to moves files from D drive to C drive using Java code (access denied is the error) but if transfered from d drive to e drive same code works

I tried following code.

public static void transferFile(String name) {
        try {
            File a = new File("D:\\" + name);
            System.out.println(path);
            File b = new File("C:\\" + name);
            input = new FileInputStream(a);
            output = new FileOutputStream(b);

            byte[] buf = new byte[1024];
            int length;
            while ((length = input.read(buf)) > 0) {
                output.write(buf, 0, length);
            }
            input.close();
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Poornima
  • 174
  • 8
Vijay
  • 11
  • 5
  • Whatever user you're running the code as doesn't have permission to write to the root of the C drive. – Matti Virkkunen Sep 01 '16 at 12:02
  • Do you have access to **C** drive?Are you the administrator? – Imran Sep 01 '16 at 12:02
  • Have you tried running your Java program as admin (in Windows) or as root (in Linux)? I don't think this is a code problem. – Tim Biegeleisen Sep 01 '16 at 12:02
  • in windows 7 if we move any file manually then also it need adminstrator permission to paste the file in C: drive so i need to know how to move files using java – Vijay Sep 01 '16 at 12:07
  • Blame Window's UAC: no writing to C: without giving your process administrative rights. since java can't elevate the process on the fly the only way is to run your java-VM "as Administrator". – piet.t Sep 01 '16 at 12:07
  • as piet mentioned it will be the UAC thing try disabling the uac and try – jan_kiran Sep 01 '16 at 12:31
  • Some steps to flow to avoid these error 1.RUn your IDE(Net beans or Eclipse) as administrator 2.Right the folder and click the properties option,then uncheck read only box – Sriram S Sep 01 '16 at 12:33
  • You might have a look at this thread http://stackoverflow.com/questions/4662574/how-do-i-elevate-my-uac-permissions-from-java – SubOptimal Sep 01 '16 at 14:17

0 Answers0