Here you can find the source of rename(File f1, File f2)
public static boolean rename(File f1, File f2)
//package com.java2s; /**//from ww w . j av a 2 s .c om * Project: zonda.logger.server * * File Created at 12-2-3 * FileUtil: FileUtil.java 12-2-3 darwin $ * * Copyright 2008 Alibaba.com Croporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * Alibaba Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Alibaba.com. */ import java.io.File; public class Main { public static boolean rename(File f1, File f2) { ensurePathExists(f2.getParent()); if (f2.exists()) f2.delete(); return f1.renameTo(f2); } public static String ensurePathExists(String path) { File f = new File(path); if (!f.exists()) f.mkdirs(); return path; } }