Move a file

 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {

  public static boolean copyFile(File source, File dest) {
    try {
      FileInputStream sourceFile = new FileInputStream(source);
      try {
        java.io.FileOutputStream destinationFile = null;
        try {
          destinationFile = new FileOutputStream(dest);
          // Lecture par segment de 0.5Mo
          byte buffer[] = new byte[512 * 1024];
          int nbLecture;
          while ((nbLecture = sourceFile.read(buffer)) != -1) {
            destinationFile.write(buffer, 0, nbLecture);
          }
        } finally {
          destinationFile.close();
        }
      } finally {
        sourceFile.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }
  public static boolean moveFile(File source, File destination) {
    if (!destination.exists()) {
      boolean result = source.renameTo(destination);
      if (!result) {
        result = true;
        result &= copyFile(source, destination);
        if (result){
          result &= source.delete();
        }          
      }
      return (result);
    } else {
      return (false);
    }
  }
}
  
Home 
  Java Book 
    Runnable examples  

IO File:
  1. Compare File Dates
  2. Compress files using with ZIP
  3. Concatenate files
  4. Copy a File with NIO FileChannel and ByteBuffer
  5. Copy a file with FileReader and FileWriter
  6. Copy a file with InputStream and OutputStream
  7. Copy a file and overwrite
  8. Delete a file
  9. Delete File Recursively
  10. Get readable file size
  11. Move a file
  12. Rename a file
  13. Report a file's status
  14. Search a file by regular expressions
  15. Touch a file: set File Last Modified Time