Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing files on Windows XP  RSS feed

 
Don Blodgett
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having some very difficult times with what I would imagine would be a very easy task.

I am trying to write a simple txt file to the "My Documents" directory on Windows XP. The odd bit is that the directory and every directory under it is marked as read-only, and the File.canWrite method is returning false as would be expected.

The problem is that Windows XP does not support the read-only attribute for directories, instead it uses it for custom folders which means files can be created in the directory. If I try to remove the read-only attribute, it does not allow it and reverts back to read-only status.

Has anyone run into this issue before, and if so how do you get around it?

Thanks for any help on this issue.
 
Peter Lawrey
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would assume you don't have permission to alter these directories.
Do you have administrator rights?
 
Don Blodgett
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I have admin rights, I can manually create the file. I am unable to create it through Java. If I instead change the directory to somewhere the read-only flag is not set (not a custom directory), I am able to create the file in Java.

Not sure how much it will help, but this is the code.

logFile is a global that is set to the a relative path of the file I want to write. I know this is correct, because in the IOException it throws I get the correct path.

The System.out is there just to show what the File.canWrite method returns



Thanks for the help.
 
Don Blodgett
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears that this is a known issue with Java and there are plans to put this in to JSE7.

Link for more info for those interested: http://jcp.org/en/jsr/detail?id=203

It states that there are native means of doing what I would need to do. So although it is undesirable, does anyone know of what these native methods are?
 
Don Blodgett
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I kind of feel stupid...

The file path was UTF-8 encoded and needed to be decoded. For those that are interested from the code I posted above the line setting the rootDir should be changed to the following

rootDir = URLDecoder.decode(new File(url.getPath()).getParentFile().getAbsolutePath(), "UTF-8");
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's funny. I was going to post that, because I had similar problems with file paths which came from encoded URLs not so long ago. But then I noticed you said:
I know this is correct, because in the IOException it throws I get the correct path.

so I stopped typing my post about URL-encoding. Just goes to show, what we know ain't always so. Anyway, it's good you got your problem solved.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!