java IO to copy one File to another

前端 未结 6 1099
别那么骄傲
别那么骄傲 2020-11-30 09:48

I have two Java.io.File objects file1 and file2. I want to copy the contents from file1 to file2. Is there an standard way to do this without me having to create a method th

6条回答
  •  爱一瞬间的悲伤
    2020-11-30 10:27

    Since Java 7 you can use Files.copy() from Java's standard library.

    You can create a wrapper method:

    public static void copy(String sourcePath, String destinationPath) throws IOException {
        Files.copy(Paths.get(sourcePath), new FileOutputStream(destinationPath));
    }
    

    that can be used in the following way:

    copy("source.txt", "dest.txt");
    

提交回复
热议问题