例子
以下示例显示java.io.File.canWrite()方法的用法。
package com.jc2182;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
boolean bool = false;
try {
// create new file
f = new File("test.txt");
// set writable to false
f.setWritable(false);
// returns boolean
bool = f.canWrite();
// print
System.out.println("Can write to test.txt: "+bool);
// set writable to true
f.setWritable(true);
// returns boolean
bool = f.canWrite();
// print
System.out.print("Can write to test.txt: "+bool);
} catch(Exception e) {
// if any I/O error occurs
e.printStackTrace();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
Can write to test.txt: false
Can write to test.txt: false