`

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损坏文件可以COPY损坏文件COPY专家

    可以COPY损坏文件可以COPY损坏文件可以COPY损坏文件

    文件copy 目录copy

    文件copy 目录copy

    Java文件操作一例:Copy 复制文件.rar

    Java文件操作一例:Copy 复制文件,虽然是复制文件,不过通过本源码你可以学习很多相关的Java基础技巧,比如Java对文件的事件处理、取得目录内容的事件处理、如何弹出文件选择器、如何得到选择文件的绝对路径、如何...

    fastcopy 文件拷贝

    Fastcopy是最快的文件拷贝工具。磁盘间相互拷贝文件是司空见惯的事情,通常情况都是利用WINDOWS自带的复制粘贴功能来实现的。这样做在数目较小的情况下倒也没什么问题,但是数目较大时,系统自带的复制功能就有些...

    Fastcopy文件拷贝copy

    Fastcopy是一款速度非常快的文件拷贝软件。Fastcopy是日本的最快的文件拷贝工具。

    FastCopy 绿色汉化单文件版

    FastCopy 是 Windows 平台上最快的文件拷贝、删除软件。由于其功能强劲,性能优越,一时间便超越相同类型的所有其他软件。由于FastCopy64位软件十分小巧,你甚至可以在安装后,直接将安装目录中的文件复制到任何可...

    copy文件的bat程序

    copy文件的bat程序 bat copy 详细的用批量执行方法。在不用开发工具进行开发要复制文件的时候那是相当有用。快速,高效的复制文件

    copy文件使用

    copy文件

    复制文件小助手 Copy Handler

    复制文件小助手 Copy Handler

    Delphi 实现copy文件

    Delphi 实现copy文件,Delphi 实现copy文件

    分割COPY合并文件小工具

    我们知道DOS 命令的COPY有合并文件的功能,如: Copy /b 1.txt+2.exe=3.jpg,如果都是文本文件,则可以直接打开分割。但是如果包含有二进制文件,则需要读取指定的位置,然后拷贝指定长度的二进制字节,并保存为另外...

    FastCopy 快速复制文件管理器

    FastCopy 文件转移 是一款非常优秀的文件管理工具,主要特色是可以将源文件的权限列表复制到目标文件。方便于在服务器上的文件共享管理。 例如:源文件原来在D分区是已经发布的有权限分配的共享文件,但现在为了方便...

    FastCopy文件拷贝软件

    文件拷贝软件通过解压后里面含有安装包及安装使用说明; fastcopy可以快速实现大文件复制粘贴,比DOS自带的robocopy更为快捷;

    iCopy2.2.4更新文件.rar

    iCopy升级文件 潜力iPhone

    FastCopy文件快速拷贝安装包

    Fastcopy是日本的最快的文件拷贝工具。磁盘间相互拷贝文件是司空见惯的事情,通常情况都是利用WINDOWS自带的复制粘贴功能来实现的。这样做在数目较小的情况下倒也没什么问题,但是数目较大时,系统自带的复制功能就...

    FastCopy,一款好用的文件和权限备份的软件,备份速度快

    FastCopy,一款好用的文件和权限备份的软件,备份速度快

    AutoCopy文件自动复制同步软件V2.1绿色免费版

    AutoCopy是一款专业的文件自动复制同步软件。AutoCopy工作原理是可以在指定的时间进行自动复制!意思就是说指定一个文件夹,然后设定时间,软件会在规定的时间内把文件复制到目标文件夹中。 在一次工作中,看到美工...

    c#大文件copy

    c#大文件copy,分段进行1024

    FastCopy高速文件复制软件

    (如果你想使用SHA-1,可以修改fastcopy.ini文件在[main]条目下添加 Using_MD5=0); 3.新增“记录窗口大小”和“记录窗口位置”菜单; 4.新增“源目录和目标目录对换”菜单。 V1.84 1.修正通配符过滤器一些问题。 V...

    文件快速复制FastCopy

    一款快速复制文件的工具,选取要复制的文件夹,选取复制到的位置,点击复制,快速完成。

Global site tag (gtag.js) - Google Analytics