注册 登录
编程论坛 数据结构与算法

一个关于File 类创建txt 的问题

z511963074 发布于 2013-05-07 22:11, 697 次点击
import *;

public class TestFile {
    public static void main(String[] args){
        String sep = File.separator;
        String fileName = "myTest.txt";
        String directory = "hzw" + sep + "dcq";
        
        File file = new File(directory, fileName);
        
        if(file.exists()){
            System.out.println("文件路径:" + file.getAbsolutePath());
            System.out.println("文件名  :" + file.getName());
            System.out.println("文件大小:" + file.length());
        }
        
        else {
            try{
                file.getParentFile().mkdirs(); //***********
                file.createNewFile();
            } catch(IOException e){
                System.out.println("创建文件失败!");
                e.printStackTrace();
            }
        }
        
    }
}

注意用 ****** 注释了的地方,
若去掉 .getParentFile(), 则创建的是名为"myTest.txt" 的空文件夹
为什么???
求大神解答。
1 回复
#2
z5119630742013-05-07 22:11
沙发~
1