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
How can I move files to another folder with java?
提问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
回答by Matt
Commons-io has a few methods in the FileUtils class that can help you.
Commons-io 在 FileUtils 类中有一些方法可以帮助您。
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)。