Custom Directory Walker import java. IOException; import java. FileAlreadyExistsException; import java. FileSystemLoopException; import java. FileVisitResult; import java. Files; import java. Path; import java. SimpleFileVisitor; import java. BasicFileAttributes; import static java. Use custom FileVisitor to recursively copy file or directories.
Two ways to achieve this are described here. The first method utilizes Files package for moving while the other method first copies the file to destination and then deletes the original copy from the source.
Using Files. Path move method: Renaming and moving the file permanently to a new location. Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Previous File Permissions in Java. Next Copying file using FileStreams in Java. Recommended Articles. Improve this question. Lernkurve You want a command line tool or code?
Add a comment. Active Oldest Votes. Improve this answer. Fangming In my case I had some subfolders and I also wanted to copy the structure, and found the method FileUtils.
Maybe this helps some other people too. I tried Files. Ethan Leroy what difference between copyDirectoryStructure and copyDirectory? As you can see here, "Directories can be copied.
However, files inside the directory are not copied, so the new directory is empty" docs. FileUtils — Jean Bob. Show 2 more comments. The following is an example of using JDK7. Archimedes Trajano Archimedes Trajano There is actually a visitor by the oracle guys for this job: docs.
Mine just copies the files, theirs is a full app with copying of attributes. With Groovy, you can leverage Ant to do: new AntBuilder.
Dario Seidl 3, 1 1 gold badge 31 31 silver badges 47 47 bronze badges. You probably can in Java, but it is like using a sledge-hammer to crack a walnut. Perhaps, but in groovy AntBuilder is part of the distribution and the automatic imports list which means it is directly available for any groovy code as written in the answer.
Is it Groovy? But it seems correct. It is good if we are copying and simultaneously doing some work with text being copied. Works, simple, good! The only difference between this solution and perhaps others is the date modified and date created of the copy are set to the current time, but sometimes that's what you want anyway. Call me old fashioned, but it's great to see curly brackets on new lines. Much more readable! I think this is the clearer approach. What about with a progress bar?
This is my piece of Groovy code for that. Gangnus Gangnus How is this better than FileUtils.
0コメント