Leon Anavi
IT Tips && Tricks

Java

Created: 11.09.2009 14:14 Last Modified: 11.09.2009 14:14 Views: 8502
Keywords: Copy, File, InputStream, OutputStream

Copy a File

Introduction

This example will demonstrate how to copy a file using Java. The algorithm is more than simple: check if the source file exists and open it for reading, create the destination file for writing and copy the content of the source file.

Console Application

By building the following code a Java console application will be created which attempts to copy a file.

Source Code

package copyfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *
 * @author leon
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try
        {
            if (2 != args.length)
            {
                throw new Exception("Enter source and destination file.");
            }
            Copy(args[0], args[1]);
        }
        catch(Exception Ex)
        {
            System.out.println( Ex.getMessage() );
        }
    }

    public static void Copy(String sFileSrc, String sFileDest)
                                    throws FileNotFoundException, IOException
    {
        //Open files
        File FileSource = new File(sFileSrc);
        //Make sure the source file really exists
        if ( false == FileSource.exists() )
        {
            String sErr = "File '";
            sErr += FileSource.getAbsolutePath();
            sErr += "' does not exists.";
            throw new FileNotFoundException(sErr);
        }
        File FileDestination = new File(sFileDest);
        //Make sure the copy will not override an existing file
        if (true == FileDestination.exists() )
        {
            String sErr = "File '";
            sErr += FileDestination.getAbsolutePath();
            sErr += "' exists.";
            throw new FileNotFoundException(sErr);
        }
        //Open source file for reading
        InputStream Input = new FileInputStream(FileSource);
        //Create destination file and prepare it for writing
        OutputStream Output = new FileOutputStream(FileDestination);
        //Copy file content
        byte[] Buffer = new byte[1024];
        int nLength = 0;
        while ( 0 < (nLength = Input.read(Buffer)) )
        {
            Output.write(Buffer, 0, nLength);
        }
        //Close both files
        Input.close();
        Output.close();
    }

}

Run the Application

Different cases of execution of the application on Linux Fedora Core 11:
  • Attempt to copy non-existing file
  • [leon@localhost dist]$ java -jar CopyFile.jar testSource testDest
    File '/home/leon/NetBeansProjects/CopyFile/dist/testSource' does not exists.
    
  • Copy files successfully
  • [leon@localhost dist]$ java -jar CopyFile.jar testSource testDest
    
  • Destination file exists
  • [leon@localhost dist]$ java -jar CopyFile.jar testSource testDest
    File '/home/leon/NetBeansProjects/CopyFile/dist/testDest' exists.
    

Class Reference

File
InputStream
OutputStream
FileNotFoundException
IOException


  Home | About | Contact | Disclaimer | Sitemap © 2009-2022 Leon Anavi. All rights reserved.