Java Rename File rename(File f1, File f2)

Here you can find the source of rename(File f1, File f2)

Description

rename

License

Open Source License

Declaration

public static boolean rename(File f1, File f2) 

Method Source Code

//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;
    }
}

Related

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