以一种独立于操作系统的方式将文件从一个文件夹复制到另一个文件夹
我有两个文件夹,例如folder1 和folder2。
folder1 包含不同类型的文件,如 .txt、.doc、.png。所有文件仅发布在此文件夹中。
现在,我想要做的是,一旦文件保存在folder1中,该文件的副本就会进入folder2中的子文件夹,并且子文件夹名称应该是该文件的扩展名。即,如果 abc.txt 文件保存在folder1中,则应在folder2内创建txt文件夹(如果txt文件夹不存在),并且应将abc.txt复制到该子文件夹内。如果我们有 xyz.doc 文件,则创建 doc 文件夹并保存在其中。我想用 Java 来做这个。 这应该是独立于平台的,即我们可以在 Window、Linux 或 Android 中复制。
I have tow folders, say folder1 and folder2.
folder1 contains files of different type like .txt, .doc, .png. All file are posted in this folder only.
Now, what i want to do is as soon as file is saved in folder1, a copy of that file goes into subfolder in folder2 and the subfolder name should be the extension of that file. i.e if abc.txt file is saved in folder1 then txt folder should be created inside folder2 (if txt folder does not exist) and abc.txt should be copied inside that subfolder. And if we have xyz.doc file then create doc folder and save inside that. I want to do this in Java.
and this should be platform independent i.e we can copy in Window, Linux or Android.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以启动一个线程来频繁检查该文件夹中是否有新文件。该线程具有文件中所有哈希值的列表。如果该文件不在列表中,则它是一个新文件。然后您可以将文件复制到新位置。您可以检查哈希值,而不是检查文件名。
You can start a thread that checks frequently for new files in that folder. The thread has a list of all hashes from the files. If the file is not in the list its a new file. Then you can copy the file to the new location. Instead of checking the name of a file you can check the hash.
你能等待 Java 7 吗? WatchService 非常适合新
文件
对象。引用这些 JavaDocs 顶部的内容:
( Oracle 的强调,我的大胆;)
Can you wait for Java 7? WatchService is perfect for notification of new
File
objects.Quote from the top of those JavaDocs:
( Emphasis by Oracle, boldness by me. ;)
您可以通过 < 来实现这一目标代码>File.list() & Common 的 FileUtils
You can achieve this one by
File.list()
& Common's FileUtils