Copy files in one folder to another

时间:2015-09-01 22:51:01

标签: java

The title is a little misleading, so I'll elaborate here: I have a directory, and in it is a lot of subdirectories, with files in those. I want to be able to copy all the files over to another directory, but not the folders.

This is what I want done:

Before:

Directory from
    folder a
         folder b
              something.txt
              folder c
                  somethingelse.txt
    folder c
         anotherthing.txt

After:

Directory to
    something.txt
    somethingelse.txt
    anotherthing.txt

I would prefer not to use any external libraries.

1 个答案:

答案 0 :(得分:3)

Since Java 7 there is a new API for the filesystem. You may use a FileWalker to collect a reference to all the FILES (not directories) and then simply copy them over using the same API. With the Path of each file you will be able to get the name of the file without keeping the rest of the path. Simmilar to getFileName() from File.