3

When I try to create a file in C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp, I get the following error:

java.io.FileNotFoundException: C:\ProgramData\Microsoft\Windows\Start Menu\Progr
ams\StartUp\test.bat (Access is denied)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at Program.testMethod(Program.java:26)
        at Program.main(Program.java:14)

I would like to know why I am getting this error and how to work around it.

When I put quotes around the path as suggested below, I get the following error:

java.io.FileNotFoundException: "C:\ProgramData\Microsoft\Windows\Start Menu\Prog
rams\StartUp\test.bat" (The filename, directory name, or volume label syntax is i
ncorrect)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at Program.testMethod(Program.java:26)
        at Program.main(Program.java:14)

Line 26 is:

BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("\"C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\StartUp\\test.bat\""), "utf-8"));
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Synchronous
  • 193
  • 1
  • 3
  • 9

2 Answers2

6

This is surely the user access on the C drive. Program data is the restricted folder and can only be accessed by the administrators having complete access to C drive.

This is the restricted area in windows not the problem with the java code. If you check you will not be able to create any new file in this StartUp folder.

To check your access on C drive:

  1. go to the path: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp.
  2. Right click, go to Properties --> Security Tab --> Select the user --> click on edit
  3. Assign proper rights and you will be able to create file there.
Dheeraj Arora
  • 608
  • 4
  • 13
1

Your original iteration without the quotes is the correct way to denote the file path but based on the exception, you most likely don't have access to create a file in the said folder - you can confirm that by opening the folder in windows explorer and manually creating a file through the right click context menu.

vijairaj
  • 310
  • 3
  • 10