The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.nio  [27 examples] > Files  [7 examples]

e172. Copying One File to Another

    try {
        // Create channel on the source
        FileChannel srcChannel = new FileInputStream("srcFilename").getChannel();
    
        // Create channel on the destination
        FileChannel dstChannel = new FileOutputStream("dstFilename").getChannel();
    
        // Copy file contents from source to destination
        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
    
        // Close the channels
        srcChannel.close();
        dstChannel.close();
    } catch (IOException e) {
    }

 Related Examples
e166. Creating a Memory-Mapped File
e167. Persisting Changes to a Memory-Mapped ByteBuffer
e168. Determining If a ByteBuffer Is Direct
e169. Reading from a Channel with a ByteBuffer
e170. Writing to a Channel with a ByteBuffer
e171. Writing and Appending a ByteBuffer to a File

See also: Byte Buffers    File Locking    Sockets    Streams   


© 2002 Addison-Wesley.