Here you can find the source of renameFile(String newName, String oldName)
static public void renameFile(String newName, String oldName) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.*; public class Main { static public void renameFile(String newName, String oldName) throws IOException { if (File.separatorChar == '\\') { copyFile(oldName, newName);//from w ww . j ava2s .co m File oldF = new File(oldName); oldF.delete(); } else { File oldF = new File(oldName); File newF = new File(newName); if (newF.exists()) { newF.delete(); } if (!oldF.renameTo(newF)) { throw new IOException(String.format("Unable to rename %s to %s", oldName, newName)); } } } static public void copyFile(String src, String dst) throws IOException { FileOutputStream fos = null; FileInputStream fis = null; try { fos = new FileOutputStream(dst); fis = new FileInputStream(src); byte[] buf = new byte[4096]; int nread = fis.read(buf); while (nread > 0) { fos.write(buf, 0, nread); nread = fis.read(buf); } } catch (IOException e) { File f = new File(dst); if (f.exists()) { f.delete(); } throw e; } finally { if (fis != null) { try { fis.close(); } catch (Exception e) { } } if (fos != null) { try { fos.close(); } catch (Exception e) { } } } } static public boolean exists(String path) { File f = new File(path); return f.exists(); } }