关于File类中的问题
我创建了一个File的文件类对象a,并调用File类中的renameTo(b)方法,而a对应的文件与b对应的文件在同一文件夹中,请问运行后a对应的文件会被覆盖吗?以下是我运行的程序:
package File;
import *;
import java.util.Date;
public class RenameFile {
//显示文件基本信息
private static void fileDate(File f){
System.out.println(
"Absolute path:"+f.getAbsolutePath()+
"\n Can read:"+f.canRead()+
"\n Can write:"+f.canWrite()+
"\n getName:"+f.getName()+
"\n getParent:"+f.getParent()+
"\n getPath"+f.getPath()+
"\n length:"+f.length()+
"\n lastModified:"+new Date(f.lastModified()));
if(f.isFile())
System.out.println("It's a file");
else if(f.isDirectory())
System.out.println("It's a directory");
}
//命令行第一个参数是原来的文件名,第二个参数是新文件名
public static void main(String []args){
File a=new File("myfile.txt");
File b=new File("file.txt");
System.out.println("The original file's information:");
fileDate(a);
a.renameTo(b);
System.out.println("\n The file information after rename:");
fileDate(b);//文件 重命名
fileDate(a);
if(!a.exists()){
System.out.println("\n The original file never exists.");
}
}
}
这是运行后的结果:
The original file's information:
Absolute path:D:\eclipse\workplace\IO.wenjianjibenxinxihuoqu\myfile.txt
Can read:true
Can write:true
getName:myfile.txt
getParent:null
getPathmyfile.txt
length:0
lastModified:Thu Jan 01 08:00:00 CST 1970
The file information after rename:
Absolute path:D:\eclipse\workplace\IO.wenjianjibenxinxihuoqu\file.txt
Can read:true
Can write:true
getName:file.txt
getParent:null
getPathfile.txt
length:14
lastModified:Tue Oct 05 13:11:49 CST 2010
It's a file
The original file never exists.
a的文件是否被覆盖了?有人知道吗?
[ 本帖最后由 tmaceye 于 2010-11-3 12:07 编辑 ]