Here you can find the source of move(File source, File destination)
Parameter | Description |
---|---|
source | file |
destination | file |
public static boolean move(File source, File destination)
//package com.java2s; // it under the terms of the GNU General Public License as published by import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; import java.util.logging.Level; import java.util.logging.Logger; public class Main { /** jdk1.4 logger */ private static Logger logger = Logger.getLogger("de.axelwernicke.mypod.util"); /**/*from w w w . jav a2 s .c o m*/ * Moves a file. * <li> check parameter * <li> check if there is something to do * <li> try to rename file instead physically moving * <li> if renaming failed, copy using nio transfer method * <li> if copy succeded, delete source, else delete destination * * @param source file * @param destination file * * @return true, if moving succeded */ public static boolean move(File source, File destination) { logger.entering("de.axelwernicke.mypod.util.FileUtils", "move"); boolean success = false; // check parameter if ((source == null) || (destination == null)) { return false; } // we only have to do something if source != destination if (source.equals(destination)) { return true; } // try renaming success = source.renameTo(destination); if (success) { if (logger.isLoggable(Level.FINE)) { logger.fine("moved file from " + source + " to " + destination + " by renaming"); } } else { // copy physically long bytesCopied = copy(source, destination); // delete source if success, destination otherwise if (bytesCopied == source.length()) { int cnt = 0; boolean srcDel = false; boolean destDel = false; // mysteriously it takes sometimes up to one minute until the source file can be deleted... do { // try to delete source srcDel = source.delete(); if (srcDel) { success = true; if (logger.isLoggable(Level.FINE)) { logger.fine("moved file from " + source + " to " + destination + " by copying"); } } // try at least to delete destination to avoid file duplication else { success = false; destDel = destination.delete(); } cnt++; logger.warning("mv " + source + " to " + destination + " " + cnt + ". src deleted: " + srcDel + " dest deleted: " + destDel); try { Thread.sleep(500); } catch (Exception e) { ; } } while (!(srcDel || destDel) && (cnt < 1000)); } else { success = false; boolean delDest = destination.delete(); logger.warning("moving file from " + source + " to " + destination + " by copying failed (copied bytes != filesize), destination file deleted: " + delDest); } if (logger.isLoggable(Level.FINE)) { logger.fine("moved file from " + source + " to " + destination + " by copying"); } } logger.exiting("de.axelwernicke.mypod.util.FileUtils", "move"); return success; } /** * Copies file using nio transfer method. * Since we get somtimes exceptions, lets try it up to ten times. * * @param source file * @param destination file * @return number of bytes copied */ public static long copy(File source, File destination) { logger.entering("de.axelwernicke.mypod.util.FileUtils", "copy"); long bytesCopied = -1; try { boolean copied = false; int tries = 0; FileChannel fic = new FileInputStream(source).getChannel(); FileChannel foc = new FileOutputStream(destination).getChannel(); do { try { bytesCopied = foc.transferFrom(fic, 0, fic.size()); copied = true; } catch (IOException e) { logger.info("copy try " + tries + " of 10 failed: " + destination + " " + e.getMessage()); tries++; } } while (!copied && (tries < 10)); fic.close(); foc.close(); } catch (Exception e) { logger.warning("exception raised :" + e.getMessage()); } logger.exiting("de.axelwernicke.mypod.util.FileUtils", "copy"); return bytesCopied; } /** * Deletes a file. * If file can not deleted now, its scheduled to be deleted on myPod exit * * @param file to be deleted * @return true, if file was successfully deleted or scheduled to be deleted on exit. */ public static boolean delete(File file) { logger.entering("de.axelwernicke.mypod.util.FileUtils", "delete"); boolean success = false; try { if (file.canRead() && file.canWrite()) { boolean deleted = file.delete(); if (!deleted) { file.deleteOnExit(); } success = true; } } catch (Exception e) { logger.warning("Exception raised: " + e.getMessage()); e.printStackTrace(); } logger.exiting("de.axelwernicke.mypod.util.FileUtils", "delete"); return success; } }