如何将文件从一个位置移动到另一个位置在java? [英] how to move file from one location to another location in java?

查看:177
本文介绍了如何将文件从一个位置移动到另一个位置在java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如何将文件从一个位置移动到另一个位置。当我运行我的程序任何在该位置创建的文件,它会自动移动到指定的位置,以及如何知道哪个文件被移动?在java中



提前感谢。

解决方案

  myFile.renameTo(new File(/ the / new / place / newName.file)); 

文件#renameTo (它不仅可以重命名,还可以在目录之间移动,至少在同一个文件系统上)


重命名由此抽象路径名表示的文件。



此方法的行为的许多方面固有地依赖于平台:重命名操作可能无法将文件从一个文件系统移动到另一个文件系统,它可能不是原子的,如果文件具有目标抽象路径名已经存在。应始终检查返回值,以确保重命名操作成功。


如果您需要一个更全面的解决方案(例如希望在磁盘之间移动文件),请查看Apache Commons FileUtils#moveFile


my question how to move a file from one location to another location. when i run my program any file created in that location it automatically move to the specified location and how to know which file is moved?.. in java

thanks in advance..

解决方案

myFile.renameTo(new File("/the/new/place/newName.file"));

File#renameTo does that (it can not only rename, but also move between directories, at least on the same file system).

Renames the file denoted by this abstract pathname.

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

If you need a more comprehensive solution (such as wanting to move the file between disks), look at Apache Commons FileUtils#moveFile

这篇关于如何将文件从一个位置移动到另一个位置在java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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