Read only file from java programing source code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javasample;
import java.io.*;
/**
*
* @author rajesh kumar shukla
*/
public class ReadOnlyfile {
public static void main(String[] a) throws IOException {
File f = new File("f");
if (!f.createNewFile()) {
System.out.println("Can't create new file.");
return;
}
if (!f.canWrite()) {
System.out.println("Can't write new file!");
return;
}
if (!f.setReadOnly()) {
System.out.println("Grrr! Can't set file read-only.");
return;
}
if (f.canWrite()) {
System.out.println("Most immutable, captain!");
System.out.println("But it still says canWrite() after setReadOnly");
return;
} else {
System.out.println("Logical, captain!");
System.out.println("canWrite() correctly returns false after setReadOnly");
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javasample;
import java.io.*;
/**
*
* @author rajesh kumar shukla
*/
public class ReadOnlyfile {
public static void main(String[] a) throws IOException {
File f = new File("f");
if (!f.createNewFile()) {
System.out.println("Can't create new file.");
return;
}
if (!f.canWrite()) {
System.out.println("Can't write new file!");
return;
}
if (!f.setReadOnly()) {
System.out.println("Grrr! Can't set file read-only.");
return;
}
if (f.canWrite()) {
System.out.println("Most immutable, captain!");
System.out.println("But it still says canWrite() after setReadOnly");
return;
} else {
System.out.println("Logical, captain!");
System.out.println("canWrite() correctly returns false after setReadOnly");
}
}
}
0 comments:
Post a Comment