Here you can find the source of move(final File from, final File to, final boolean replace)
public final static File move(final File from, final File to, final boolean replace) throws IOException
//package com.java2s; /*//from w w w. ja v a 2 s . c o m * This file is part of Skript. * * Skript 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. * * Skript 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 Skript. If not, see <http://www.gnu.org/licenses/>. * * * Copyright 2011-2014 Peter G?ttinger * */ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; public class Main { private static boolean RUNNINGJAVA6 = true; public final static File move(final File from, final File to, final boolean replace) throws IOException { if (!replace && to.exists()) throw new IOException( "Can't rename " + from.getName() + " to " + to.getName() + ": The target file already exists"); if (!RUNNINGJAVA6) { if (replace) Files.move(from.toPath(), to.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); else Files.move(from.toPath(), to.toPath(), StandardCopyOption.ATOMIC_MOVE); } else { File moveTo = null; if (replace && to.exists()) { moveTo = new File(to.getAbsolutePath() + ".old0"); int i = 0; while (moveTo.exists() && i < 1000) moveTo = new File(to.getAbsolutePath() + ".old" + (++i)); if (i == 999 || !to.renameTo(moveTo)) throw new IOException("Can't rename " + from.getName() + " to " + to.getName() + ": Cannot temporarily rename the target file"); } if (!from.renameTo(to)) { if (moveTo != null) moveTo.renameTo(to); throw new IOException("Can't rename " + from.getName() + " to " + to.getName()); } if (moveTo != null) moveTo.delete(); } return to; } }