
/* |
* ProcessFileName.java |
* |
* Created on 2006年8月22日, 下午3:29 |
* |
* 文件名的处理 |
*/ |
package study.iostudy; |
import java.io.*; |
/* |
* 文件名的处理 |
* String getName(); 获得文件的名称,不包含文件所在的路径。 |
* String getPath(); 获得文件的路径。 |
* String getAbsolutePath(); 获得文件的绝对路径。 |
* String getParent(); 获得文件的上一级目录的名称。 |
* String renameTo(File newName); 按参数中给定的完整路径更改当前的文件名。 |
* int compareTo(File pathName); 按照字典顺序比较两个文件对象的路径。 |
* boolean isAbsolute(); 测试文件对象的路径是不是绝对路径。 |
*/ |
public class ProcesserFileName { |
public static void main(String[] args) { |
File fileObject1 = new File("d:\\mydir\\firstFile.txt"); |
File fileObject2 = new File("d:\\firstFile.txt"); |
boolean pathAbsolute = fileObject1.isAbsolute(); |
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * "); |
System.out |
.println("There are some information of fileObject1's file name:"); |
System.out.println("fileObject1: " + fileObject1); |
System.out.println("fileObject2: " + fileObject2); |
System.out.println("file name: " + fileObject1.getName()); |
System.out.println("file path: " + fileObject1.getPath()); |
System.out.println("file absolute path: " |
+ fileObject1.getAbsolutePath()); |
System.out.println("file's parent directory: " |
+ fileObject1.getParent()); |
System.out.println("file's absoulte path: " + pathAbsolute); |
int sameName = fileObject1.compareTo(fileObject2); |
System.out.println("fileObject1 compare to fileObject2: " + sameName); |
fileObject1.renameTo(fileObject2); |
System.out.println("file's new name: " + fileObject1.getName()); |
} |
} |



