Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: unmark file is read-only

Tags:

java

windows

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

like image 321
Zombies Avatar asked May 20 '10 19:05

Zombies


3 Answers

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.");
}
like image 106
corsiKa Avatar answered Sep 22 '22 09:09

corsiKa


public boolean setWritable(boolean writable)

like image 23
stacker Avatar answered Sep 26 '22 09:09

stacker


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.

like image 23
Pindatjuh Avatar answered Sep 22 '22 09:09

Pindatjuh