Here you can find the source of renameFile(String strSrc, String strDest, boolean deleteIfExist)
Parameter | Description |
---|---|
strSrc | String |
strDest | String |
deleteIfExist | boolean |
Parameter | Description |
---|---|
IOException | an exception |
// ////////////////////////////////////////////////////// public static boolean renameFile(String strSrc, String strDest, boolean deleteIfExist) throws IOException
//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; } }