This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.io.FileNotFoundException: C:\textfile.txt (Access is denied)

 
justin smythhe
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is access denied ??? I suspect this is a Windows 7 security related problem. How do i solve this problem ?

java.io.IOException: Access is denied
Caught exception - Cannot create your file !!!
java.io.FileNotFoundException: C:\textfile.txt (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:180)
at java.io.FileOutputStream.<init>(FileOutputStream.java:122)
at java.io.FileWriter.<init>(FileWriter.java:62)

 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I found the problem. I checked by trying to save a text file right to that location manually and got a "you don't have permission error".

You can't save directly to the C drive like that I think.

Normally, since most Computers have a Users directory underneath the C drive, yours might not but the chances are high that it does,

then you would make a new Directory under the users directory, you might be able to just create it programmatically.


Anyway,

try creating it as C:/Users/SomeDirectory/textfile.txt


At the very least, you need a subdirectory underneath C. You can't just save something as a file itself to the C drive itself.

Sorry, scratch that, normally under the Users directory, you have a directoy that is your computer name. I wouldn't know what yours is.

so it's

C:/Users/YourComputerName/textfile.txt

that will work.



 
justin smythhe
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I did that and it works. Is there a way of making our code "create new files/folders as an administrator" ?

Thanks
 
Campbell Ritchie
Sheriff
Posts: 48409
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you actually want code running on your computer in administrator mode? It is all right if you write it yourself, but what if somebody else wrote it . . .
 
justin smythhe
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Do you actually want code running on your computer in administrator mode? It is all right if you write it yourself, but what if somebody else wrote it . . .

Oh ! yes.
 
Campbell Ritchie
Sheriff
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have worked out how to do it, please tell this chap how to get accepted by a firewall, too
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic