Java Rename File rename(File file, String newName, boolean overwite)

Here you can find the source of rename(File file, String newName, boolean overwite)

Description

rename

License

Apache License

Declaration

public static File rename(File file, String newName, boolean overwite) 

Method Source Code

//package com.java2s;
/*/*w w w. ja v  a2s  .  c o m*/
 * JEF - Copyright 2009-2010 Jiyi (mr.jiyi@gmail.com)
 *
 * Licensed 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.File;

public class Main {

    public static File rename(File file, String newName, boolean overwite) {
        File target = new File(file.getParentFile(), newName);
        if (target.exists()) {
            if (overwite) {
                if (!target.delete())
                    return null;
            } else {
                return null;
            }
        }
        return file.renameTo(target) ? target : null;
    }
}

Related

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