The Java Developers Almanac 1.4

 
Webexampledepot.com

   
Home > List of Packages > java.io  [37 examples] > Files  [10 examples]

e1071. Copying One File to Another

This example uses file streams to copy the contents of one file to another file. See e172 Copying One File to Another for an example that uses file channels.
    // 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();
    }

 Related Examples
e19. Determining If a File or Directory Exists
e20. Creating a File
e21. Getting the Size of a File
e22. Deleting a File
e23. Creating a Temporary File
e24. Renaming a File or Directory
e25. Moving a File or Directory to Another Directory
e26. Getting and Setting the Modification Time of a File or Directory
e27. Forcing Updates to a File to the Disk

See also: Directories    Encodings    Filenames and Pathnames    Parsing    Reading and Writing    Serialization   


© 2002 Addison-Wesley.