Java tutorial
//package com.java2s; import java.io.File; public class Main { /** * * @param fi * @param newName * @return */ public static File renameTo(File fi, String newName) { /* File.renameTo is a abstract file rename finction . * I override this function which return File but boolean. * To get the new File obj , you can separately rename the file you want to . * Example code: * File f = new File("D:\\abc.def") ; File tmp = renameTo(f,"a.a") ; renameTo(tmp,"b.b") ; */ //It can rename the File to the same dir. String dest = fi.getParent(); dest += "\\" + newName; //System.out.println("Rename To : "+dest) ; File newNameFile = new File(dest); fi.renameTo(newNameFile); return newNameFile; } }