java 如何使用java将文件移动到另一个文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27931444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:38:57  来源:igfitidea点击:

How can I move files to another folder with java?

javafile

提问by Sebastian Tare B.

I want to move files (images) from a folder to another:

我想将文件(图像)从一个文件夹移动到另一个文件夹:

For example:

例如:

/home/folder1/image.png

/home/folder1/image.png

to

/home/folder1/folder2/image.png

/home/folder1/folder2/image.png

And obviously remove the image from the folder1

并且显然从folder1中删除图像

I've trying to do it by reading the path and then modifying it, or using renameTo, but i can't do it.

我试图通过读取路径然后修改它或使用 renameTo 来做到这一点,但我做不到。

I hope someone can help me a little with this, Thanks.

我希望有人可以帮助我一点,谢谢。

EDIT:

编辑:

Well I can put the code but it's simple to explain what i did:

好吧,我可以放代码,但很容易解释我所做的:

I just created a Folder class that has a File object of my folder (/home/folder1) , i read all the images inside and save it in an File array, then i scan it and try to change the path of every image file String to another

我刚刚创建了一个 Folder 类,它具有我的文件夹 (/home/folder1) 的 File 对象,我读取里面的所有图像并将其保存在一个 File 数组中,然后我扫描它并尝试更改每个图像文件 String 的路径到另一个

EDIT:

编辑:

Thanks to all for the help, all are good examples, I was able to change my files to another location, there was a bunch of files I wanted to move so, I didn't want to create too many objects.

感谢所有人的帮助,都是很好的例子,我能够将我的文件更改到另一个位置,我想移动一堆文件,所以我不想创建太多对象。

采纳答案by haley

You said you tried renameTo and it didn't work, but this worked for me. After I renamed it I deleted the original file.

你说你试过 renameTo 但它没有用,但这对我有用。重命名后,我删除了原始文件。

File a = new File("C:\folderA\A.txt");
a.renameTo(new File("C:\folderB\" + a.getName()));
a.delete();

回答by SummerCode

The usual approach to solving this is copying the file and then deleting it from the original location, but you can follow this tutorialfor more information. Also, the platform(linux, windows, is not important).

解决此问题的常用方法是复制文件,然后将其从原始位置删除,但您可以按照本教程获取更多信息。此外,平台(linux,windows,并不重要)。

回答by Matt

Commons-io has a few methods in the FileUtils class that can help you.

Commons-io 在 FileUtils 类中有一些方法可以帮助您。

http://commons.apache.org/proper/commons-io/javadocs/api-release/index.html?org/apache/commons/io/package-summary.html

http://commons.apache.org/proper/commons-io/javadocs/api-release/index.html?org/apache/commons/io/package-summary.html

Example: FileUtils.moveFile(src, dest);

示例: FileUtils.moveFile(src, dest);

回答by fmardini

I didn't run this, but it should work

我没有运行这个,但它应该可以工作

File f1 = new File("/home/folder1/image.png");
File f2 = new File("/home/folder1/folder2/image.png");

f1.renameTo(f2);

回答by Trinity

There are many approaches for you to do that. This snippet is one of them, you can move your files like this way:

有很多方法可以让您做到这一点。这个片段就是其中之一,您可以像这样移动文件:

try {
    final File myFile = new File("C:\folder1\myfile.txt");
    if(myFile.renameTo(new File("C:\folder2\" + myFile.getName()))) {
        System.out.println("File is moved successful!");
    } else {
        System.out.println("File is failed to move!");
    }
}catch(Exception e){
    e.printStackTrace();
}

回答by Angelo Wentzler

In java 8+ you can simply use Files.movefrom nio:

在 java 8+ 中,您可以简单地使用nio 中的Files.move

try {
    Path source = Paths.get("/home/folder1/image.png");
    Path dest = Paths.get("/home/folder1/folder2/image.png");
    Files.move(source, dest);
} catch (IOException e) {
    ...
}

The paths can even come from different file system providers (ie a ZipFileSystem).

路径甚至可以来自不同的文件系统提供者(即 ZipFileSystem)。