Java Rename File rename(File from)

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

Description

rename

License

Open Source License

Declaration

public static File rename(File from) throws IOException, InterruptedException 

Method Source Code


//package com.java2s;
/*/*  www.j  a v  a  2s  .  co m*/
 * Copyright (c) 2014 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */

import java.io.File;
import java.io.IOException;

public class Main {
    public static File rename(File from) throws IOException, InterruptedException {
        File to = new File(from.getParentFile(), from.getName() + "." + System.currentTimeMillis());
        rename(from, to);
        return to;
    }

    public static void rename(File from, File to) throws IOException, InterruptedException {
        if (from.exists()) {
            // Files.move(Paths.get(from.toString()), Paths.get(to.toString()));

            for (int i = 0; i < 200; i++) {
                if (from.renameTo(to)) {
                    return;
                }

                Thread.sleep(10);
            }
        }

        throw new IOException(
                "Could not rename '" + from.getAbsolutePath() + "' to '" + to.getAbsolutePath() + "'");
    }
}

Related

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