Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

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 "io/ioutil"
import "os"
func copy(dst, src string) error {
	data, err := ioutil.ReadFile(src)
	if err != nil {
		return err
	}
	stat, err := os.Stat(src)
	if err != nil {
		return err
	}
	return ioutil.WriteFile(dst, data, 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 "io/ioutil"
import "os"
func copy(dst, src string) error {
	data, err := ioutil.ReadFile(src)
	if err != nil {
		return err
	}
	stat, err := os.Stat(src)
	if err != nil {
		return err
	}
	err = ioutil.WriteFile(dst, data, stat.Mode())
	if err != nil {
		return err
	}
	return os.Chmod(dst, stat.Mode())
}
new File(src).withInputStream { input ->
    new File(dst).withOutputStream {output ->
        output << input
    }
}

new File(dst).bytes = new File(src).bytes
const { copyFileSync } = require('fs');
copyFileSync(src, 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...
< >
programming-idioms.org