How to copy a file to a directory in Java 7

后端 未结 3 1201
抹茶落季
抹茶落季 2021-02-06 02:37

I\'m trying to copy a number of files to an output directory in Java 7 using Path and Files. This doesn\'t work:

Files.copy(Paths.get(\"/my/file.txt\"), Paths.ge         


        
相关标签:
3条回答
  • 2021-02-06 03:04

    From docs Java 7:

    copy(Path source, Path target, CopyOption... options)

    Copy a file to a target file.

    So you must specify destination file.

    I have a large number of files

    You can get file name by splitting source path and append to destination folder.

    0 讨论(0)
  • 2021-02-06 03:08

    The easiest way:

    Path file = /* path to source file */
    Path to = /* path to destination directory */
    Files.copy(file, to.resolve(file.getFileName()));
    
    0 讨论(0)
  • 2021-02-06 03:18

    The command appears to be attempting to replace the directory itself. Try specifying the filename in the target directory

    Files.copy(Paths.get("/my/file.txt"), Paths.get("/my/output/directory/file.txt"));
    
    0 讨论(0)
提交回复
热议问题