Java Rename File rename(File source, File dest)

Here you can find the source of rename(File source, File dest)

Description

It seems that java doesn't like renaming files across partitions (i.e.

License

Open Source License

Declaration

public static void rename(File source, File dest) throws IOException 

Method Source Code


//package com.java2s;
/* {{{ GPL/*w  w  w  .  j a  va 2  s. c  o m*/
    
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
}}} */

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

public class Main {
    /**
     * It seems that java doesn't like renaming files across partitions (i.e. if
     * a rename actually requires a copy and a delete, it just returns false).
     * This method attempts to use java's rename, and if that fails, it does its
     * own copy and delete.
     */
    public static void rename(File source, File dest) throws IOException {
        if (!source.renameTo(dest)) {

            FileInputStream from = null;
            FileOutputStream to = null;

            try {
                from = new FileInputStream(source);
                to = new FileOutputStream(dest);

                stream(from, to);

            } finally {
                if (from != null) {
                    try {
                        from.close();
                    } catch (IOException e) {
                        // nothing
                    }
                }

                if (to != null) {
                    try {
                        to.close();
                    } catch (IOException e) {
                        // nothing
                    }
                }
            }

            source.delete();
        }

    }

    public static void stream(InputStream from, OutputStream to) throws IOException {

        byte[] buffer = new byte[4096];
        int bytesRead;

        while ((bytesRead = from.read(buffer)) != -1) {
            to.write(buffer, 0, bytesRead); // write
        }

        to.flush();
    }
}

Related

  1. rename(File from, File to)
  2. rename(File from, File to, boolean overwrite)
  3. rename(File oldFile, File newFile)
  4. rename(File pFrom, File pTo)
  5. rename(File pFrom, File pTo, boolean pOverWrite)
  6. rename(File source, File target)
  7. rename(File src, File dest)
  8. rename(File src, String name)
  9. rename(File srcFile, String newName)