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

博主分享了在Java中如何将图片从一个目录移动到另一个目录并删除源文件的问题。他们创建了一个Folder类,遍历文件,尝试使用renameTo方法,但未成功。最终,通过修改代码,实现了文件的移动和源文件的删除,例如移动/image.png到/folder1/folder2/image.png并从/folder1删除。
摘要由CSDN通过智能技术生成

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

For example:

/home/folder1/image.png

to

/home/folder1/folder2/image.png

And obviously remove the image from the folder1

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.

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

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.

解决方案

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值