copy文件方式一

关于copy文件,一般都用流来读取文件,然后再写入另一个文件。

  下面用java NIO包中的FileChannel 类中提供的transferFrom方法来进行copy文件,该方法比比Apache IO包中提供的要快。
  copy原理代码如下:
    File file = new File("D:/workspace/OJ平台/MyProject/src/test3.xml");
       
        File outFile = new File("D:/workspace/OJ平台/MyProject/src/aaa.xml");
        if (outFile.exists())
        {
            outFile.delete();
        }
       
        FileInputStream inStream = new FileInputStream(file);
        FileOutputStream outStream = new FileOutputStream(outFile);
       
        outStream.getChannel().transferFrom(inStream.getChannel(), 0, Long.MAX_VALUE);
       
       
        System.out.println(outFile.length() == file.length());

标准结构的代码如下 :
   public static void copyFile(File srcFile, File destFile) throws IOException
    {
        if (!srcFile.exists())
        {
            throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
        }
       
        if (srcFile.isDirectory())
        {
            throw new IOException("Source '" + srcFile + "' exists but is a directory");
        }
       
        if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath()))
        {
            throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same");
        }
       
        if (destFile.exists())
        {
            if (destFile.delete())
            {
                if (!destFile.createNewFile())
                {
                    throw new IOException("create file " + destFile.getAbsolutePath() + " failed.");
                }
            }
            else
            {
                throw new IOException("delete file " + destFile.getAbsolutePath() + " failed.");
            }
        }
       
        if ((destFile.getParentFile() != null) && (!destFile.getParentFile().exists())
            && (!destFile.getParentFile().mkdirs()))
        {
            throw new IOException("Destination '" + destFile + "' directory cannot be created");
        }
       
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try
        {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            long timeCopy = System.currentTimeMillis();
            fos.getChannel().transferFrom(fis.getChannel(), 0, Long.MAX_VALUE);
            if (logger.isDebugEnable())
            {
                timeCopy = System.currentTimeMillis() - timeCopy;
                logger.debug("copy file " + srcFile.getAbsolutePath() + " cost " + timeCopy + " ms.");
            }
        }
        finally
        {
            CloseUtils.closeQuietly(fos);
            CloseUtils.closeQuietly(fis);
        }
       
        if (srcFile.length() != destFile.length())
        {
            throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
        }
       
        if (destFile.setLastModified(srcFile.lastModified()))
        {
            if (logger.isDebugEnable())
            {
                logger.debug("set file '" + srcFile.getAbsolutePath() + "' lastModified failure.");
            }
        }
    }

使用程序来copy文件,需要重新生成一个文件,因此该文件的最后修改日期是该文件的创建日期,与源文件是不相同的, 因此destFile.setLastModified(srcFile.lastModified())) 使用该方法是不能修改文件的最后修改日期的但在windows上面copy文件,生成新文件的最后修改日期与原文件的最后修改日期是一致的。
       

你可能感兴趣的:(copy)