/* |
* SetterFileAttribute.java |
* |
* Created on 2006年8月22日, 下午3:51 |
* |
* 测试和设置文件属性 |
*/ |
package study.iostudy; |
import java.io.*; |
public class SetterFileAttribute |
{ |
/* |
* File类中提供的有关文件属性测试方面的方法有以下几种: |
* boolean exists(); 测试当前文件对象指示的文件是否存在。 |
* boolean isFile(); 测试当前文件对象是不是文件。 |
* boolean isDirectory(); 测试当前文件对象是不是目录。 |
* boolean canRead(); 测试当前文件对象是否可读。 |
* boolean canWrite(); 测试当前文件对象是否可写。 |
* boolean setReadOnly(); 将当前文件对象设置为只读。 |
* long length(); 获得当前文件对象的长度。 |
*/ |
public static void main(String[] args) |
{ |
File dirObject = new File( "d:\\mydir" ); |
File fileObject = new File( "d:\\mydir\\firstFile.txt" ); |
try |
{ |
dirObject.mkdir(); |
fileObject.createNewFile(); |
} catch (IOException e) |
{ |
e.printStackTrace(); |
} |
System.out.println( "* * * * * * * * * * * * * * * * * * * * * * * * " ); |
System.out.println( "there are some information about property of file object:" ); |
System.out.println( "file object : " + fileObject); |
System.out.println( "file exist? " + fileObject.exists()); |
System.out.println( "Is a file? " + fileObject.isFile()); |
System.out.println( "Is a directory?" + fileObject.isDirectory()); |
System.out.println( "Can read this file? " + fileObject.canRead()); |
System.out.println( "Can write this fie? " + fileObject.canWrite()); |
long fileLen = fileObject.length(); |
System.out.println( "file length: " +fileLen); |
boolean fileRead = fileObject.setReadOnly(); |
System.out.println(fileRead); |
System.out.println( "Can read this file? " + fileObject.canRead()); |
System.out.println( "Can write this fie? " + fileObject.canWrite()); |
System.out.println( "* * * * * * * * * * * * * * * * * * * * * * * * " ); |
} |
} |