开发者

Copying file from one folder to another in an OS independent way

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开发者_Go百科 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.


You can achieve this one by File.list() & Common's FileUtils


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.


Can you wait for Java 7? WatchService is perfect for notification of new File objects.

Quote from the top of those JavaDocs:

A watch service that watches registered objects for changes and events. For example a file manager may use a watch service to monitor a directory for changes so that it can update its display of the list of files when files are created or deleted.

( Emphasis by Oracle, boldness by me. ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜