如何使用Java将文件移动到另一个文件夹? [英] How can I move files to another folder with java?

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

问题描述

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

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

例如:

/home/folder1/image.png

/home/folder1/image.png

/home/folder1/folder2/image.png

/home/folder1/folder2/image.png

显然从文件夹1中删除了图像

And obviously remove the image from the folder1

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

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

我希望有人能对此有所帮助,谢谢.

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

我可以放置代码,但是很容易解释我所做的事情:

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

我刚刚创建了一个Folder类,其中包含我的文件夹(/home/folder1)的File对象,我读取了其中的所有图像并将其保存在File数组中,然后进行扫描并尝试更改每个文件的路径图片文件的字符串到另一个

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

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

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.

推荐答案

您说您尝试过namedTo,但它不起作用,但这对我有用.重命名后,我删除了原始文件.

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

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

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

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