为什么File.renameTo(...)不创建目的地的子目录? [英] Why doesn't File.renameTo(...) create sub-directories of destination?

查看:317
本文介绍了为什么File.renameTo(...)不创建目的地的子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不 File.renameTo(...) 创建目标文件路径中包含的子目录?

Why doesn't File.renameTo(...) create sub-directories contained in the destination file path?

例如,

File source = new File(System.getProperty("user.dir") + 
                "/src/MyFolder/MyZipFolder.zip");
File dest = new File(System.getProperty("user.dir") + 
                "/src/MyOtherFolder/MyZipFolder.zip");
System.out.println(source.renameTo(dest));


由于MyOtherFolder不存在,因此它将始终返回false.为了使它起作用,我必须通过以编程方式(即mkdirs())或手动创建它们来确保所有子目录都存在.为什么没有将此功能包含在此方法中?


Since MyOtherFolder does not exist, this will always return false. In order for this to work, I have to ensure that all sub-directories exist either by creating them programmatically(i.e. mkdirs()), or manually. Is there a reason why this functionality was not included in this method?

推荐答案

当前的File API在Java中的实现不是很好.当前不存在的File API中有许多功能是理想的,例如移动,复制和检索文件元数据.

The current File API isn't very well implemented in Java. There is a lot of functionality that would be desirable in a File API that isn't currently present such as move, copy and retrieving file metadata.

我不认为有人会向您提供有关为何按原样编写API的答案.可能是一份较差的初稿,由于当时的向后兼容性问题而无法使用.

I don't think anyone will be able to give you an answer as to why the API is written as is. Probably a poor first draft that went live and couldn't be changed due to backwards compatibility issues.

在即将发布的Java 7中已经解决了这些问题.已经创建了一个全新的API,用于处理文件

These issue have been addressed in the upcoming Java 7. A entirely new API has been created to deal with files java.nio.file.Files.

这篇关于为什么File.renameTo(...)不创建目的地的子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆