Java Rename File renameFileHard(File srcFile, File destFile)

Here you can find the source of renameFileHard(File srcFile, File destFile)

Description

rename File Hard

License

Open Source License

Declaration

public static boolean renameFileHard(File srcFile, File destFile)
        throws IOException 

Method Source Code

//package com.java2s;
/*//from ww  w.j a  v  a  2s  .c om
 * $Id$
 * --------------------------------------------------------------------------------------
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**
     * @see org.mule.util.FileUtils#renameFileHard
     */
    public static boolean renameFileHard(File srcFile, File destFile)
            throws IOException {
        boolean isRenamed = false;
        if (srcFile != null && destFile != null) {
            if (!destFile.exists()) {
                if (srcFile.isFile()) {
                    FileInputStream in = null;
                    FileOutputStream out = null;
                    try {
                        in = new FileInputStream(srcFile);
                        out = new FileOutputStream(destFile);
                        out.getChannel().transferFrom(in.getChannel(), 0,
                                srcFile.length());
                        isRenamed = true;
                    } finally {
                        if (in != null) {
                            in.close();
                        }
                        if (out != null) {
                            out.close();
                        }
                    }
                    if (isRenamed) {
                        srcFile.delete();
                    } else {
                        destFile.delete();
                    }
                }
            }
        }
        return isRenamed;
    }
}

Related

  1. renameFile(File src, File dest)
  2. renameFile(String source, String dest)
  3. renameFile(String sourcePath, String targetPath)
  4. renameFile(String src_pathname, String dest_pathname)
  5. renameFileFromChinese(File file)
  6. renameFileIfExists(File f)
  7. renameFileOnWindows(File oldFile, File newFile)
  8. renameFiles(File fileDirectory, String regex, String replacement)
  9. renameFiles(String rootDir, String sourcePostfix, String destPostfix)