Copy Files Using I/O Streams – Java I/O: Part II

Copy Files Using I/O Streams

The Files class provides methods to copy files using byte I/O streams from the java.io package (ยง20.2, p. 1234). Bytes can be copied from a source file to an Input-Stream and from an OutputStream to a destination file. We demonstrate copying where input streams and output streams are assigned to files. See also reading and writing files using paths (p. 1314).

The code below reads bytes from the input file project/src/pkg/Util.java using a BufferedInputStream and writes the bytes to the output file path project/backup/ Util.java.

Click here to view code image

String inputFileName  = “project/src/pkg/Util.java”;
Path outputFilePath = Path.of(“project”, “backup”, “Util.java”);
try (var fis = new FileInputStream(inputFileName);
     var bis = new BufferedInputStream(fis)) {
  long bytesCopied = Files.copy(bis, outputFilePath,
                                StandardCopyOption.REPLACE_EXISTING);
  System.out.println(“Bytes copied: ” + bytesCopied);      // Bytes copied: 103
}

The code below copies bytes from the input file path project/backup/Util.java to the output file project/archive/src/pkg/Util.java using a BufferedOutputStream.

Click here to view code image

Path inputFilePath  = Path.of(“project”, “backup”, “Util.java”);
String outputFileName = “project/archive/src/pkg/Util.java”;
try (var fos = new FileOutputStream(outputFileName);
     var bos = new BufferedOutputStream(fos)) {
  long bytesCopied = Files.copy(inputFilePath, bos);
  System.out.println(“Bytes copied: ” + bytesCopied);      // Bytes copied: 103
}

The following statement can be used to print the contents of a file to the standard output:

Click here to view code image

Files.copy(inputFilePath, System.out);     // Prints file content to standard out.

In general, any InputStream or OutputStream can be used in the respective copy() methods.

Moving and Renaming Directory Entries

The move() method of the Files class implements moving and renaming directory entries. The method can be configured by specifying copy options.

The move() method emulates the copying behavior of the copy() method. But in contrast to the copy() method, the move() method deletes the source if the operation succeeds. Both methods allow the destination to be overwritten, if the constant StandardCopyOption.REPLACE_EXISTING is specified.

Click here to view code image

static Path move(Path source, Path destination, CopyOption… options)
            throws IOException

Moves or renames the source to the destination. After moving, the source is deleted. The method returns the path to the destination. The default behavior is outlined below, but can be configured by copy options:

  • If destination already exists, the move fails.
  • If source and destination are the same, the method has no effect.
  • If source is a symbolic link, the target of the link is moved.
  • If source is an empty directory, the empty directory is moved to the destination.

The following copy options can be specified to configure the default moving behavior:

  • StandardCopyOption.REPLACE_EXISTING: If the destination exists, this option indicates to replace the destination if it is a file or an empty directory. If the destination exists and is a symbolic link, this option indicates to replace the symbolic link and not its target.
  • StandardCopyOption.ATOMIC_MOVE: The move is performed as an atomic file system operation. It is implementation specific whether the move is performed if the destination exists or whether an IOException is thrown. If the move cannot be performed, the method throws an AtomicMoveNotSupportedException.
Categories: ,

Leave a Reply

Your email address will not be published. Required fields are marked *