2

Hi how can I get rid of C drive access with java codes I can do this with manual but I want to do this with java codes

here is my code

File file=new File("C:\\Windows\\b.txt");
    if (!file.exists()) {
        try {

            file.createNewFile();
        } catch (IOException e) {
            System.out.println("file not created and");
            e.getMessage();
            e.printStackTrace();
        }

And my exceptions

java.io.IOException: Access is denied
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:1006)
at tekrar.Write.main(Write.java:26)`
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

2 Answers2

2

you need to run the program as an administrator to edit the C:\Windows folder. this is a system folder that you should not access.

Bary12
  • 1,060
  • 9
  • 23
0

You cannot write anything in the system folder without administrator privileges in windows. As I understand you cannot request administrator access from within a Java program and you'll have to RUN the application in admin mode. See this question for some tips: Run Java file as Administrator with full privileges

P.S. Do you really need to write into the windows folder?

Community
  • 1
  • 1
dimoniy
  • 5,820
  • 2
  • 24
  • 22