Java Rename File rename(File from, File to)

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

Description

Renames file, with checking errors and 3 seconds retry against external programs (like antivirus or TortoiseSVN) locking.

License

Open Source License

Declaration

public static void rename(File from, File to) throws IOException 

Method Source Code

//package com.java2s;
/* ************************************************************************
 *
 *  TMPotter - Bi-text Aligner/TMX Editor.
 *
 *  Copyright (C) 2005-2009 Raymond: Martin
 *            (C) 2015 Hiroshi Miura//www.  j a v  a 2  s.c  o  m
 *
 *  Copyright (C) 2008 Alex Buloichik
 *                2009 Didier Briel,  2012 Alex Buloichik, Didier Briel
 *                2014 Alex Buloichik, Aaron Madlon-Kay
 *
 *  This file is part of TMPotter.
 *
 *  TMPotter 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  TMPotter 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 TMPotter.  If not, see http://www.gnu.org/licenses/.
 *
 * *************************************************************************/

import java.io.File;

import java.io.IOException;

public class Main {
    public static final long RENAME_RETRY_TIMEOUT = 3000;

    /**
     * Renames file, with checking errors and 3 seconds retry against external
     * programs (like antivirus or TortoiseSVN) locking.
     */
    public static void rename(File from, File to) throws IOException {
        if (!from.exists()) {
            throw new IOException("Source file to rename (" + from + ") doesn't exist");
        }
        if (to.exists()) {
            throw new IOException("Target file to rename (" + to + ") already exists");
        }
        long bfor = System.currentTimeMillis();
        while (!from.renameTo(to)) {
            long end = System.currentTimeMillis();
            if (end - bfor > RENAME_RETRY_TIMEOUT) {
                throw new IOException("Error renaming " + from + " to " + to);
            }
        }
    }
}

Related

  1. rename(File file, String newName, boolean overwite)
  2. rename(File file, String prefix, String name, HashMap optional)
  3. rename(File from)
  4. rename(File from, File to)
  5. rename(File from, File to)
  6. rename(File from, File to)
  7. rename(File from, File to)
  8. rename(File from, File to)
  9. rename(File from, File to, boolean overwrite)