Difference between revisions of "Copy a file"

From CodeCodex

 
(9 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
==Implementations==
 
==Implementations==
===Java===
+
These code examples demonstrate how to copy a system file.
 +
===Erlang===
 
<pre>
 
<pre>
 +
% copy files by name
 +
file:copy("/path/to/file", "/path/to/other/phile").
 +
 +
% copy files by io_device
 +
{ok, ID1} = file:open("/path/to/file", [read]).
 +
{ok, ID2} = file:open("/path/to/other/phile", [write]).
 +
file:copy(ID1, ID2).
 +
ok = file:close(ID1).
 +
ok = file:close(ID2).
 +
</pre>
 +
 +
===Java===
 +
<pre class="java">
 
     // Copies src file to dst file.
 
     // Copies src file to dst file.
 
     // If the dst file does not exist, it is created
 
     // If the dst file does not exist, it is created
Line 18: Line 32:
 
     }
 
     }
 
</pre>
 
</pre>
* Original Source: [http://javaalmanac.com/egs/java.io/pkg.html?l=rel#Files The Java Developers Almanac 1.4]
 
  
 
Copies or moves a file without a loop.  Although I'm not sure, I believe using a FileChannel allows the OS to short-circuit the operation (without actually transferring the data through Java code).
 
Copies or moves a file without a loop.  Although I'm not sure, I believe using a FileChannel allows the OS to short-circuit the operation (without actually transferring the data through Java code).
  
<HIGHLIGHTSYNTAX>
+
<pre class="java">
 
     public void copy(File src, File dest, boolean move) throws Exception {
 
     public void copy(File src, File dest, boolean move) throws Exception {
 
// Create channel on the source
 
// Create channel on the source
Line 39: Line 52:
 
         if(move) src.delete();
 
         if(move) src.delete();
 
     }
 
     }
</HIGHLIGHTSYNTAX>
+
</pre>
  
 +
===Perl===
 +
<pre class="perl">
 +
use File::Copy;
 +
 +
# copy files by name
 +
copy('/path/to/file', '/path/to/other/phile') or die "Copy failed: $!";
 +
 +
# copy filehandle references
 +
copy($src, $dst) or die "Copy failed: $!";
 +
</pre>
 +
 +
===Python===
 +
<pre class="python">
 +
import shutil
 +
 +
# copy files by name
 +
shutil.copyfile('/path/to/file', '/path/to/other/phile')
 +
 +
# copy file-objects
 +
shutil.copyfileobj(src, dst)
 +
</pre>
 +
 +
===Ruby===
 +
<pre class="ruby">
 +
require 'fileutils'
 +
 +
# copy files by name
 +
src = '/path/to/file'
 +
dst = '/path/to/other/phile'
 +
FileUtils.copy src, dst
 +
 +
# copy file-objects
 +
open(src, "r") do |f1|
 +
  open(dst, "w") do |f2|
 +
    FileUtils.copy_stream f1, f2
 +
  end
 +
end
 +
</pre>
 +
 +
=== Tcl ===
 +
<pre class="tcl">
 +
file copy /path/to/file /path/to/other/phile
 +
</pre>
  
 +
[[Category:Erlang]]
 
[[Category:Java]]
 
[[Category:Java]]
 +
[[Category:Perl]]
 +
[[Category:Python]]
 +
[[Category:Ruby]]
 +
[[Category:Tcl]]
 
[[Category:I/O]]
 
[[Category:I/O]]

Latest revision as of 04:41, 6 January 2012

Implementations[edit]

These code examples demonstrate how to copy a system file.

Erlang[edit]

% copy files by name
file:copy("/path/to/file", "/path/to/other/phile").

% copy files by io_device
{ok, ID1} = file:open("/path/to/file", [read]).
{ok, ID2} = file:open("/path/to/other/phile", [write]).
file:copy(ID1, ID2).
ok = file:close(ID1).
ok = file:close(ID2).

Java[edit]

    // Copies src file to dst file.
    // If the dst file does not exist, it is created
    void copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);
    
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

Copies or moves a file without a loop. Although I'm not sure, I believe using a FileChannel allows the OS to short-circuit the operation (without actually transferring the data through Java code).

    public void copy(File src, File dest, boolean move) throws Exception {
	// Create channel on the source
        FileChannel srcChannel = new FileInputStream(src).getChannel();

    	// Create channel on the destination
    	FileChannel dstChannel = new FileOutputStream(dest).getChannel();
    
    	// Copy file contents from source to destination
	dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
    
    	// Close the channels
	srcChannel.close();
        dstChannel.close();

        if(move) src.delete();	
    }

Perl[edit]

use File::Copy;

# copy files by name
copy('/path/to/file', '/path/to/other/phile') or die "Copy failed: $!";

# copy filehandle references
copy($src, $dst) or die "Copy failed: $!";

Python[edit]

import shutil

# copy files by name
shutil.copyfile('/path/to/file', '/path/to/other/phile')

# copy file-objects
shutil.copyfileobj(src, dst)

Ruby[edit]

require 'fileutils'

# copy files by name
src = '/path/to/file'
dst = '/path/to/other/phile'
FileUtils.copy src, dst

# copy file-objects
open(src, "r") do |f1|
  open(dst, "w") do |f2|
    FileUtils.copy_stream f1, f2
  end
end

Tcl[edit]

file copy /path/to/file /path/to/other/phile