Java File Move nui moveFile(String source, String target)

Here you can find the source of moveFile(String source, String target)

Description

move File

License

Open Source License

Declaration

public static void moveFile(String source, String target) throws Exception 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.io.*;

import java.nio.channels.FileChannel;

public class Main {
    public static void moveFile(String source, String target) throws Exception {
        copyFile(source, target);/*from   w w  w  .ja  v  a2s  .  c o m*/

        deleteFile(source);
    }

    public static void copyFile(String source, String target) throws Exception {
        copyFile(new File(source), new File(target));
    }

    /**
     * fast file copy
     *
     * @param source source file
     * @param target target file
     * @throws Exception if error occurs
     */
    public static void copyFile(File source, File target) throws Exception {

        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(target);
        FileChannel inChannel = fis.getChannel();
        FileChannel outChannel = fos.getChannel();

        // inChannel.transferTo(0, inChannel.size(), outChannel);
        // original -- apparently has trouble copying large files on Windows

        // magic number for Windows, 64Mb - 32Kb
        int maxCount = (64 * 1024 * 1024) - (32 * 1024);
        long size = inChannel.size();
        long position = 0;
        while (position < size) {
            position += inChannel.transferTo(position, maxCount, outChannel);
        }

        inChannel.close();
        outChannel.close();
        fis.close();
        fos.close();
    }

    public static void deleteFile(String source) throws Exception {
        new File(source).delete();
    }
}

Related

  1. move(String sourceFile, String targetFile)
  2. moveFile(File from, File to)
  3. moveFile(File source, File destination)
  4. moveFile(File src, File dest)
  5. moveFile(final File srcFile, final File destFile)
  6. moveFile(String sourceFile, String destFile)
  7. moveFiles(File src, String newSrc, File dest)
  8. mover(String file, String caminhoNovo)