标题:关于File类中的问题
只看楼主
tmaceye
Rank: 1
等 级:新手上路
帖 子:3
专家分:3
注 册:2010-10-18
 问题点数:0 回复次数:0 
关于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 编辑 ]
搜索更多相关主题的帖子: File 
2010-11-03 01:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-324808-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.078847 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved