java IO to copy one File to another

前端 未结 6 1117
别那么骄傲
别那么骄傲 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:41

    In Java 7 you can use Files.copy() and very important is: Do not forget to close the OutputStream after creating the new file.

    OutputStream os = new FileOutputStream(targetFile);
    Files.copy(Paths.get(sourceFile), os);
    os.close();
    

提交回复
热议问题