压缩包内是近180多个针对Java初学者编写的简单实例
源代码在线查看: filedemo.java
//运用File类的常用方法,获取有关文件属性的信息。
import java.io.File;
class FileDemo
{
static void OutOper(String s)
{
System.out.println(s);
}
public static void main(String args[]) {
File f1 = new File("/java/COPYRIGHT");
//调用File类的toString方法输出对象f1的路径
System.out.println(f1);
//获取该文件的各个属性和信息
OutOper("File Name: " + f1.getName());
OutOper("Path: " + f1.getPath());
OutOper("Abs Path: " + f1.getAbsolutePath());
OutOper("Parent: " + f1.getParent());
OutOper(f1.exists() ? "exists" : "does not exist");
OutOper(f1.canWrite() ? "is writeable" : "is not writeable");
OutOper(f1.canRead() ? "is readable" : "is not readable");
OutOper("is " + (f1.isDirectory() ? "" : "not" + " a directory"));
OutOper(f1.isFile() ? "is normal file" : "might be a named pipe");
OutOper(f1.isAbsolute() ? "is absolute" : "is not absolute");
OutOper("File last modified: " + f1.lastModified());
OutOper("File size: " + f1.length() + " Bytes");
//将 f1对象改名为"newFile"
File f2 = new File("newFile");
OutOper("\n"+"****now rename the file " + f1 +" to " +f2 +"****");
f1.renameTo(f2);
OutOper("name"+f2.getName());
//检查改名前的那个文件是否存在
OutOper(f1+"is exist?" +f1.exists());
//删除改名后的文件
OutOper("\n"+"****delete"+f2+"****");
f2.delete();
OutOper(f2 + "exist?" + f2.exists());
}
}