Good afternoon, when you try to unload resources from the system disk C: \

InputStream in = Main.class.getResourceAsStream("scr/resources/sp/Sp.exe"); DataInputStream din = new DataInputStream(in); FileOutputStream outF = new FileOutputStream("C:/SP"); byte[] buffer = new byte[1024]; int count = 0; while ((count=din.read(buffer)) != -1){ outF.write(buffer,0,count); } outF.flush(); outF.close(); 

in the created folder produces the following error:

 Exception in thread "main" java.io.FileNotFoundException: C:\SP (Отказано в доступе) at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(FileOutputStream.java:270) at java.io.FileOutputStream.<init>(FileOutputStream.java:213) at java.io.FileOutputStream.<init>(FileOutputStream.java:101) at com4j_example.Main.main(Main.java:89) 

and so with any place of discharge.

  • You do not have permission to write to this directory. If you try to write to C: \ Users \ <your user> \, then you should succeed. - Andrew Bystrov

2 answers 2

Sometimes this happens when you try to write to a folder, not to a file. Especially C: / SP, without a file extension like a folder. Check if there is a place to be this situation and unsubscribe.

  • Yes, I definitely forgot everything correctly to add the file name and extension, but now it gives Exception in thread "main" java.lang.NullPointerException at java.io.DataInputStream.read (DataInputStream.java:100) at com4j_example.Main.main (Main.java : 94) - Varg Sieg
  • one
    See what Main.class.getResourceAsStream returns, if it gives null, then null also gets into the DataInputStream, hence the exception. If I'm right, then you need to figure out whether you are setting the path to the file correctly and using getResourceAsStream (unfortunately, I never worked with it, so I will not try to guess the problem right away) - Uraty
 new FileOutputStream("C:/SP"); 

Windows prohibits writing to the root of the system disk without admin rights.

And the suspicion is caused by the direction of the slash

  • Everything is fine with slashes, otherwise the development environment swears) - Varg Sieg