The Java Version Almanac
javaalmanac.io
Feedback on this page?

Copy Files and IO Streams

The Files class offers a couple of convenience methods to copy data between files, InputStreams and OutputStreams. Also streams can be directly transferred with InputStream.transferTo(). Many Java idioms with temporary buffers shuffling data chunks in while loops are obsolete nowadays.

Since Java 9

import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.HexFormat; public class CopyFiles { static final HexFormat HEX = HexFormat.of(); static final byte[] TEST_CONTENT = HEX.parseHex("cafebabe"); public static void main(String... args) throws IOException { var srcfile = Files.createTempFile("CopyFiles", ".txt"); var dstfile = Files.createTempFile("CopyFiles", ".txt"); Files.write(srcfile, TEST_CONTENT); // Directly copy files Files.copy(srcfile, dstfile, StandardCopyOption.REPLACE_EXISTING); System.out.println(HEX.formatHex(Files.readAllBytes(dstfile))); // Copy data from InputStream to a file: var in = new ByteArrayInputStream(TEST_CONTENT); Files.copy(in, dstfile, StandardCopyOption.REPLACE_EXISTING); System.out.println(HEX.formatHex(Files.readAllBytes(dstfile))); // Copy the file content to an OutputStream: var out = new ByteArrayOutputStream(); Files.copy(srcfile, out); System.out.println(HEX.formatHex(out.toByteArray())); // Copy content from an InputStream to an OutputStream: in = new ByteArrayInputStream(TEST_CONTENT); out = new ByteArrayOutputStream(); in.transferTo(out); System.out.println(HEX.formatHex(out.toByteArray())); } }

This snippet at GitHub