Java Rename File rename(File from, File to, boolean overwrite)

Here you can find the source of rename(File from, File to, boolean overwrite)

Description

rename

License

Apache License

Declaration

public static synchronized void rename(File from, File to, boolean overwrite) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    public static synchronized void rename(File from, File to, boolean overwrite) throws IOException {
        boolean success = true;
        if (!from.exists()) {
            throw new IOException("file from=[" + from.getAbsolutePath() + "] not exists.");
        }/*w  ww.java2  s  .  co m*/
        if (to.exists()) {
            if (overwrite) {
                success = to.delete();
                if (!success) {
                    throw new IOException("rename from->temp failed. from=[" + from.getAbsolutePath() + "]");
                }
            } else {
                throw new IOException("file to=" + to.getAbsolutePath() + "] already exists.");
            }
        }

        success = from.renameTo(to);
        if (!success) {
            throw new IOException("rename from->temp failed. from=[" + from.getAbsolutePath() + "]");
        }
    }
}

Related

  1. rename(File from, File to)
  2. rename(File from, File to)
  3. rename(File from, File to)
  4. rename(File from, File to)
  5. rename(File from, File to)
  6. rename(File oldFile, File newFile)
  7. rename(File pFrom, File pTo)
  8. rename(File pFrom, File pTo, boolean pOverWrite)
  9. rename(File source, File dest)