Java tutorial
//package com.java2s; import java.io.File; import java.io.IOException; public class Main { private static File commit(String basename, File tmpFile) throws IOException { try { final String backupName = basename + "~"; final File desired = new File(basename); final File backup = new File(backupName); backup.delete(); if (desired.exists()) { if (!desired.renameTo(new File(backupName))) throw new IOException("can't rename to " + backupName); } if (!tmpFile.renameTo(new File(basename))) throw new IOException("can't rename to " + basename); } catch (IOException x) { tmpFile.delete(); throw x; } return new File(basename); } }