Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #228 Copy a file

Copy the file at path src to dst.

with Ada.Directories;
Ada.Directories.Copy_File (Source_Name => Src,
                           Target_Name => Dst);
System.IO
File.Copy(src, dst, true); 
import 'dart:io';
File(src).copySync(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
import "os"
func copy(dst, src string) error {
	data, err := os.ReadFile(src)
	if err != nil {
		return err
	}
	stat, err := os.Stat(src)
	if err != nil {
		return err
	}
	err = os.WriteFile(dst, data, stat.Mode())
	if err != nil {
		return err
	}
	return os.Chmod(dst, stat.Mode())
}
import "io"
import "os"
func copy(dst, src string) error {
	f, err := os.Open(src)
	if err != nil {
		return err
	}
	defer f.Close()
	stat, err := f.Stat()
	if err != nil {
		return err
	}
	g, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, stat.Mode())
	if err != nil {
		return err
	}
	defer g.Close()
	_, err = io.Copy(g, f)
	if err != nil {
		return err
	}
	return os.Chmod(dst, stat.Mode())
}
import "os"
func copy(dst, src string) error {
	data, err := os.ReadFile(src)
	if err != nil {
		return err
	}
	stat, err := os.Stat(src)
	if err != nil {
		return err
	}
	return os.WriteFile(dst, data, stat.Mode())
}
new File(dst).bytes = new File(src).bytes
new File(src).withInputStream { input ->
    new File(dst).withOutputStream {output ->
        output << input
    }
}

const { copyFileSync } = require('fs');
copyFileSync(src, dst);
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Files.copy(Path.of(src), Path.of(dst));
uses FileUtil;
Success := CopyFile(src, dst);
use File::Copy 'cp';
cp($src, $dst) or die $!;
import shutil
shutil.copy(src, dst)
require 'fileutils'
FileUtils.copy(src, dst)
use std::fs;
fs::copy(src, dst).unwrap();

New implementation...