Java FileInputStream Copy copyFile(String srcFileName, String destFileName)

Here you can find the source of copyFile(String srcFileName, String destFileName)

Description

Copy a file.

License

Open Source License

Parameter

Parameter Description
srcFileName Name of source file to copy.
destFileName Name of destination file to which to copy.

Return

true if copy successful.

Declaration


public static boolean copyFile(String srcFileName, String destFileName) 

Method Source Code


//package com.java2s;
/*   Please see the license information at the end of this file. */

import java.io.*;

public class Main {
    /**   Copy a file.//from  ww w  .j a  v  a2s . co  m
     *
     *   @param   srcFileName      Name of source file to copy.
     *   @param   destFileName   Name of destination file to which to copy.
     *
     *   @return               true if copy successful.
     */

    public static boolean copyFile(String srcFileName, String destFileName) {
        //   Assume copy fails.

        boolean result = false;

        //   Copy buffer.

        byte[] buffer = new byte[128000];

        try {
            //   See if the source file exists.

            File srcFile = new File(srcFileName);

            if (!srcFile.exists()) {
                throw new IOException("File " + srcFileName + " not found.");
            }
            //   See if the source is a directory.
            //   Error if so, as we only copy files.

            if (srcFile.isDirectory()) {
                throw new IOException("File " + srcFileName + " is a directory.");
            }
            //   Get source file modification time.

            long srcFileTime = srcFile.lastModified();

            //   Make sure we can write to the
            //   destination file.  Create any
            //   needed subdirectories along the way.

            if (!createPathForFile(destFileName)) {
                throw new IOException("Cannot create path for " + destFileName);
            }
            //   Open input stream for source.

            InputStream inputStream = new FileInputStream(srcFile);

            //   Open output stream for destination.

            OutputStream outputStream = new FileOutputStream(destFileName);

            //   Copy source characters to
            //   destination in buffer size chunks.

            int nread = 0;

            do {
                nread = inputStream.read(buffer, 0, buffer.length);

                if (nread > 0) {
                    outputStream.write(buffer, 0, nread);
                }
            } while (nread >= 0);

            //   Close source and destination files.

            inputStream.close();

            outputStream.flush();
            outputStream.close();

            //   Set modification time of destination
            //   file to match that of source file.

            File destFile = new File(destFileName);

            result = destFile.setLastModified(srcFileTime);

            result = result && (destFile.lastModified() == srcFileTime);
        } catch (IOException e) {
            //         System.out.println(
            //            "*** error in copying file: " +e.getMessage() );
        }

        return result;
    }

    /**   Create directory for specified file name.
     *
     *   @param   fileName   File name for which to create
     *                  parent directory, if necessary.
     *
     *   @return            True if directory created successfully.
     */

    public static boolean createPathForFile(String fileName) {
        //   Assume directory creation fails.

        boolean result = false;

        //   Get the file name.

        File file = new File(fileName);

        //   Get the parent path for the
        //   file name, and try to create it
        //   if necessary.
        if (file != null) {
            result = createPath(new File(file.getAbsoluteFile().getParent()));
        }

        return result;
    }

    /**   Create intermediate directories for a directory path.
     *
     *   @param   directory   The subdirectory for which to create any
     *                  missing intermediate directories.
     *
     *   @return            true if all the intermediate directories
     *                  were created successfully, or the directory
     *                  already exists.
     */

    public static boolean createPath(File directory) {
        //   Assume the directory does not exist.

        boolean result = false;

        //   Do nothing id directory is null.

        if (directory != null) {
            //   See if directory is a directory.
            //   If it is not, it could be either
            //   because it is a file, or it does
            //   not exist.

            result = directory.isDirectory();

            if (!result) {
                //   If the directory is a file,
                //   delete the file first.

                if (directory.exists())
                    directory.delete();

                //   Create all the intermediate
                //   directories.

                result = directory.mkdirs();
            }
        }

        return result;
    }
}

Related

  1. copyFile(String src, String to)
  2. copyFile(String srcDir, String destDir)
  3. copyFile(String srcFile, String destFile)
  4. copyFile(String srcFile, String dstFile)
  5. copyFile(String srcFile, String dstFile)
  6. copyFile(String srcFilename, String dtsFilename)
  7. copyFile(String srcFilePath, String destFilePath)
  8. copyFile(String srcPath, String dstPath)
  9. copyFile(String srcPath, String dstPath, boolean replace)