tags:

views:

61

answers:

4

Can I do this on Java? I'm using windows...

+2  A: 

http://java.sun.com/j2se/1.6.0/docs/api/java/io/File.html#setReadOnly%28%29

File file = new File("foo.bar");
if(file.setReadOnly()) {
    System.out.println("Successful");
}
else {
    System.out.println("All aboard the fail train.");
}

Before Java6, you could not undo this. To get around this, they put in File.setWritable(boolean) which can be used as so

File file = new File("foo.bar");
if(file.setWritable(false)) {
    System.out.println("Successful");
}
else {
    System.out.println("All aboard the fail train.");
}

if(file.setWritable(true)) {
    System.out.println("Re-enabled writing");
}
else {
    System.out.println("Failed to re-enable writing on file.");
}
glowcoder
A: 

Hi there,

take a look at this page:

http://www.javalobby.org/java/forums/t17056.html

MUG4N
+1  A: 

public boolean setWritable(boolean writable)

stacker
+1  A: 
final File f = new File(...);
f.setWritable(true);

Will change premissions to writable (not read-only).

Note: this may not work all the times, as the underlying FileSystem may deny the request. But it works on most files on your hard drives.

Pindatjuh
Don't you mean `f.setWritable(false);` to make it read only?
glowcoder
@Glowcoder: Maybe I've misinterpret the "unmark file is read-only". I thought it means to make it *not* "read-only", which is writable, which is `true`, right?
Pindatjuh
@Pind oh wow, you might just be right there. Perhaps it is I who misinterpreted the question.
glowcoder
Yes, that's how I eventually interpreted the question too. It's phrased terribly, and the question itself is split between the title and the body. I'd downvote it but I don't want to spend the rep.
Stephen P