Here you can find the source of rename(File from, File to)
public static boolean rename(File from, File to)
//package com.java2s; /*//from ww w . j a v a2 s . com * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import java.io.Closeable; 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 { public static boolean rename(File from, File to) { if (!from.renameTo(to)) { if (copy(from, to)) { if (!delete(from, true /* deleteRoot */)) { return false; } } else { return false; } } return true; } public static boolean copy(File from, File to) { boolean result = true; if (from.isDirectory()) { if (!to.isDirectory()) { if (!to.mkdirs()) { return false; } File[] files = from.listFiles(); if (files == null) { return false; } for (int i = 0; i < files.length; i++) { result &= copy(files[i], new File(to, files[i].getName())); } } } else { InputStream input = null; OutputStream output = null; try { input = new FileInputStream(from); output = new FileOutputStream(to); byte[] buffer = new byte[4096]; for (int i = input.read(buffer); i > -1; i = input.read(buffer)) { output.write(buffer, 0, i); } } catch (IOException e) { return false; } finally { if (!closeSilently(output)) { result = false; } if (!closeSilently(input)) { result = false; } } } return result; } public static boolean delete(File root, boolean deleteRoot) { boolean result = true; if (root.isDirectory()) { File[] files = root.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { result &= delete(files[i], true); } else { result &= files[i].delete(); } } } if (deleteRoot) { if (root.exists()) { result &= root.delete(); } } return result; } static boolean closeSilently(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException exception) { // Ignore; nothing we can do about this... return false; } } return true; } }