Android File Rename renameFile(String strSrc, String strDest)

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

Description

Rename file from src to des, override if des exist

Parameter

Parameter Description
strSrc source file
strDest destination file

Return

true if succees, otherswise false

Declaration

// //////////////////////////////////////////////////////
public static boolean renameFile(String strSrc, String strDest) 

Method Source Code

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

public class Main {
    /**//  w w w  . j av  a  2 s  .c  o  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. rename(String oldName, String newName)
  2. renameAs(String pathDir, String name, String wishName)
  3. renameFile(File source, File dest)
  4. renameFile(String origPath, String destPath)
  5. renameFile(String source, String dest)
  6. renameFiles(File file, String filterRegex, String replaceRegex, String replacement)
  7. renameFiles(File file, String regex, String replacement)
  8. renameFile(File file, File newFile, boolean isDeleteExistDestFile)