如何在Java中将文件从一个位置移动到另一个位置?
如何将文件从一个位置移动到另一个位置? 当我运行我的程序时,在该位置创建的任何文件都会自动移动到指定位置。 我如何知道移动了哪个文件?
提前致谢!
pmad asked 2019-06-20T22:48:51Z
10个解决方案
91 votes
myFile.renameTo(new File("/the/new/place/newName.file"));
File#renameTo可以做到这一点(它不仅可以重命名,还可以在目录之间移动,至少在同一个文件系统上)。
重命名此抽象路径名表示的文件。
此方法行为的许多方面本质上依赖于平台:重命名操作可能无法将文件从一个文件系统移动到另一个文件系统,它可能不是原子的,如果具有目标抽象路径名的文件可能不会成功 已经存在。 应始终检查返回值以确保重命名操作成功。
如果您需要更全面的解决方案(例如想要在磁盘之间移动文件),请查看Apache Commons FileUtils #moveverFile
Thilo answered 2019-06-20T22:49:24Z
53 votes
使用Java 7或更高版本,您可以使用Files.move(from, to, CopyOption... options)。
例如。
Files.move(Paths.get("/foo.txt"), Paths.get("bar.txt"), StandardCopyOption.REPLACE_EXISTING);
有关详细信息,请参阅文件文档
micha answered 2019-06-20T22:49:56Z
5 votes
要移动文件,您还可以使用Jakarta Commons IOs FileUtils.moveFile
出错时会抛出一个IOException,所以当没有抛出异常时你知道文件被移动了。
DerMike answered 2019-06-20T22:50:27Z
4 votes
Java IO的File.renameTo可用于在Java中移动文件。 另见这个问题。
Piotr answered 2019-06-20T22:50:52Z
4 votes
只需添加源文件夹和目标文件夹路径。
它会将所有文件和文件夹从源文件夹移动到目标文件夹。
File destinationFolder = new File("");
File sourceFolder = new File("");
if (!destinationFolder.exists())
{
destinationFolder.mkdirs();
}
// Check weather source exists and it is folder.
if (sourceFolder.exists() && sourceFolder.isDirectory())
{
// Get list of the files and iterate over them
File[] listOfFiles = sourceFolder.listFiles();
if (listOfFiles != null)
{
for (File child : listOfFiles )
{
// Move files to destination folder
child.renameTo(new File(destinationFolder + "\\" + child.getName()));
}
// Add if you want to delete the source folder
sourceFolder.delete();
}
}
else
{
System.out.println(sourceFolder + " Folder does not exists");
}
manjeet lama answered 2019-06-20T22:51:23Z
2 votes
您可以为该任务执行外部工具(如Windows环境中的File#renameTo),但为了保持代码的可移植性,一般方法是:
将源文件读入内存
将内容写入新位置的文件
删除源文件
只要源和目标位置在同一卷上,File#renameTo就可以正常工作。 我个人不会用它来将文件移动到不同的文件夹。
Andreas_D answered 2019-06-20T22:52:15Z
2 votes
试试这个 :-
boolean success = file.renameTo(new File(Destdir, file.getName()));
Vijay Gupta answered 2019-06-20T22:52:40Z
2 votes
Java 6
public boolean moveFile(String sourcePath, String targetPath) {
File fileToMove = new File(sourcePath);
return fileToMove.renameTo(new File(targetPath));
}
Java 7(使用NIO)
public boolean moveFile(String sourcePath, String targetPath) {
boolean fileMoved = true;
try {
Files.move(Paths.get(sourcePath), Paths.get(targetPath), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
fileMoved = false;
e.printStackTrace();
}
return fileMoved;
}
Pranav V R answered 2019-06-20T22:53:11Z
1 votes
Files.move(source, target, REPLACE_EXISTING);
您可以使用Files对象
阅读更多关于文件
Daniel Taub answered 2019-06-20T22:53:43Z
0 votes
写了这个方法,在我自己的项目中只使用替换文件(如果有现有逻辑)来做这件事。
// we use the older file i/o operations for this rather than the newer jdk7+ Files.move() operation
private boolean moveFileToDirectory(File sourceFile, String targetPath) {
File tDir = new File(targetPath);
if (tDir.exists()) {
String newFilePath = targetPath+File.separator+sourceFile.getName();
File movedFile = new File(newFilePath);
if (movedFile.exists())
movedFile.delete();
return sourceFile.renameTo(new File(newFilePath));
} else {
LOG.warn("unable to move file "+sourceFile.getName()+" to directory "+targetPath+" -> target directory does not exist");
return false;
}
}
Joel M answered 2019-06-20T22:54:08Z