Java File Move nui moveFiles(File src, String newSrc, File dest)

Here you can find the source of moveFiles(File src, String newSrc, File dest)

Description

Moves the source file to the destination file (similar to #copyFiles(File,File) but with deletion)

License

Open Source License

Parameter

Parameter Description
src The source
newSrc The new source
dest The destination

Return

The result

Declaration

public static boolean moveFiles(File src, String newSrc, File dest) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.IOException;
import java.nio.file.*;

public class Main {
    /**//from ww w  .  ja va 2 s. c  o m
     * Moves the source file to the destination file (similar to {@link #copyFiles(File, File)} but with deletion)<br>
     *
     * @param src    The source
     * @param newSrc The new source
     * @param dest   The destination
     * @return The result
     */
    public static boolean moveFiles(File src, String newSrc, File dest) {
        boolean r = false;

        if (!dest.exists()) {
            r = dest.mkdirs();
        }

        File[] files = src.listFiles();
        if (files == null) {
            try {
                Files.move(src.toPath(), new File(dest, newSrc).toPath(), StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }

        for (File srcFiles : files) {
            if (srcFiles.isDirectory()) {
                r = moveFiles(srcFiles, newSrc, new File(dest, srcFiles.getName()));
                continue;
            }

            try {
                Files.move(srcFiles.toPath(), new File(dest, srcFiles.getName()).toPath(),
                        StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return r;
    }
}

Related

  1. moveFile(File source, File destination)
  2. moveFile(File src, File dest)
  3. moveFile(final File srcFile, final File destFile)
  4. moveFile(String source, String target)
  5. moveFile(String sourceFile, String destFile)
  6. mover(String file, String caminhoNovo)