Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to UnDo java.io.File.setReadOnly() state?  RSS feed

 
Jaunty John
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program which can set a file attribute to Read Only. (Actually it can do an entire sub directory.) This code snippet shows how:
public static void doDSubDir(File filName) {
if (filName.isFile() && filName.canWrite()) {
filName.setReadOnly();
}
else if (filName.isDirectory()) {
// more code to handle this...
}
}
How can I UNDO setReadOnly() programmatically? The java.io.File class does not have a setWritable() (or anything similar). I have seen several suggestions (both on Java Ranch and Sun.JDC site) to look at the java.FilePermission class, but this doesn't seem to apply. And yet, I can't believe that a file cannot be set back to a writable state.
Does anyone have any suggestions, or preferably, code snippets.
Thanks
JohnB
 
Roy Tock
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not absolutely sure it'll do the job...but check out the FilePermission class (since JDK 1.2).
 
Jaunty John
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roy,
May I suggest that you read my question before you shoot from the hip. It saves on the cost of ammo.
Thanks
JohnB
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't seen a setWrite() method, the only way I know how is using FilePermission, PermissionCollection, and Permission objects.
I'm curious as to why these aren't applicable here...
[ July 24, 2003: Message edited by: jason adam ]
 
Jaunty John
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the File class, there is a setReadOnly() method. However, there is NOT a setWritable() method. I constanly see reference to the FilePermission class to solve this problem, BUT HAVE YET TO SEE A CODE SNIPPET EXAMPLE. And the Sun.Java documentation is so dense as to be meaningless. Has anyone seen a CODE SNIPPET (repeat, CODE SNIPPET) to accomplish the task of setting a FILE ATTRIBUTE TO A WRITABLE STATE?
Thanks in advance
JohnB
p.s. This seems to be a common question. I see another example posted on Moose(Java)Ranch today, and have never seen an answer, here or elsewhere. A CODE SNIPPET would fix this... jlb
p.p.s. My reference to another posting of this question was NOT about my other posting, but to other MooseRanch users qustions...
Thanks, May the Farce be with you...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!