Android File Delete renameFile(String strSrc, String strDest, boolean deleteIfExist)

Here you can find the source of renameFile(String strSrc, String strDest, boolean deleteIfExist)

Description

Rename file from source to destination

Parameter

Parameter Description
strSrc String
strDest String
deleteIfExist boolean

Exception

Parameter Description
IOException an exception

Return

boolean

Declaration

// //////////////////////////////////////////////////////
public static boolean renameFile(String strSrc, String strDest,
        boolean deleteIfExist) throws IOException 

Method Source Code

//package com.java2s;
import java.io.*;

public class Main {
    /**/*  w w w  .  java  2 s .  co  m*/
     * Rename file from source to destination
     * 
     * @param strSrc
     *            String
     * @param strDest
     *            String
     * @param deleteIfExist
     *            boolean
     * @return boolean
     * @throws IOException
     * @author Thai Hoang Hiep
     */
    // //////////////////////////////////////////////////////
    public static boolean renameFile(String strSrc, String strDest,
            boolean deleteIfExist) throws IOException {
        File flSrc = new File(strSrc);
        File flDest = new File(strDest);
        if (flSrc.getAbsolutePath().equals(flDest.getAbsolutePath()))
            return false;
        if (flDest.exists()) {
            if (deleteIfExist)
                flDest.delete();
            else
                throw new IOException("File '" + strDest
                        + "' already exist");
        }
        return flSrc.renameTo(flDest);
    }

    /**
     * Rename file from src to des, override if des exist
     * 
     * @param strSrc
     *            source file
     * @param strDest
     *            destination file
     * @return true if succees, otherswise false
     * @author Thai Hoang Hiep
     */
    // //////////////////////////////////////////////////////
    public static boolean renameFile(String strSrc, String strDest) {
        File flSrc = new File(strSrc);
        File flDest = new File(strDest);
        if (flSrc.getAbsolutePath().equals(flDest.getAbsolutePath()))
            return true;
        if (flDest.exists())
            flDest.delete();
        return flSrc.renameTo(flDest);
    }

    /**
     * 
     * @param strCurrenDir
     *            String
     * @param strFileName
     *            String
     * @return String
     */
    // //////////////////////////////////////////////////////
    public static String getAbsolutePath(String strCurrenDir,
            String strFileName) {
        if (!strFileName.startsWith("/") && !strFileName.startsWith("\\")) {
            if (!strCurrenDir.endsWith("/") && !strCurrenDir.endsWith("\\"))
                return strCurrenDir + "/" + strFileName;
            return strCurrenDir + strFileName;
        }
        return strFileName;
    }
}

Related

  1. forceDelete(String file)
  2. forceDelete(final File file)
  3. recursiveDelete(File fileOrDir)
  4. recursiveDelete(File path)
  5. removeableFile(File folder, String fileName)
  6. safelyDeleteFile(File filePath)
  7. safelyDeleteFile(String filePath)
  8. deleteFolderFile(String filePath, boolean deleteThisPath)
  9. deleteFile(File f)