Logo

Programming-Idioms

  • Fortran
  • Rust
  • Java

Idiom #228 Copy a file

Copy the file at path src to dst.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Files.copy(Path.of(src), Path.of(dst));
character, dimension(n_buff) :: buffer
open (newunit=u_r,file="src", action="read", form="unformatted", &
       access="stream")
open(newunit=u_w,file="dst", action="write", form="unformatted",&
     access="stream") 
inquire(unit=u_r,size=sz)
do while (sz > 0)
   n_chunk = min(sz, n_buff)
   read (unit=u_r) buffer(1:n_chunk)
   write (unit=u_w) buffer(1:n_chunk)
   sz = sz - n_chunk
end do

This opens the files, gets the file size and copies with size buffer or the rest of the file, whichever is smaller.
use std::fs;
fs::copy(src, dst).unwrap();
with Ada.Directories;
Ada.Directories.Copy_File (Source_Name => Src,
                           Target_Name => Dst);

New implementation...