简体   繁体   English

Java 文件复制到非空目标文件夹

[英]Java file copy to non empty destination folder

I'm having real difficulties figuring out how this needs to be coded without using FileUtils import.我很难弄清楚如何在不使用 FileUtils 导入的情况下对其进行编码。 I have found thousands of tutorials on how to move files to empty folders, that's easy.我找到了数千个关于如何将文件移动到空文件夹的教程,这很容易。 The difficulty is finding out how Java can move files to directories that already have files in the folder.难点在于找出 Java 如何将文件移动到文件夹中已有文件的目录。 As I understand it the REPLACE_EXISTING parameter means it will overwrite identical file names if detected in the destination directory, but the directory doesn't have a file with a matching name of the file I'm attempting to copy/move.据我了解,REPLACE_EXISTING 参数意味着如果在目标目录中检测到它,它将覆盖相同的文件名,但该目录没有与我尝试复制/移动的文件名称匹配的文件。 What am I missing?我错过了什么? How can I make this happen?我怎样才能做到这一点?

java.nio.file.DirectoryNotEmptyException occuring. java.nio.file.DirectoryNotEmptyException 发生。

enter code here

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;



public class Move {
 static File source = new File("sourcefolderhere");
static File destination = new File("destfolderhere");


public static void move(File src, File dest) throws IOException {
    Files.move(src.toPath().toAbsolutePath(), dest.toPath().toAbsolutePath(),
            StandardCopyOption.REPLACE_EXISTING);         
}

public static void main(String[] args) throws IOException { 

try {       
    if(source.isDirectory() && destination.isDirectory()) {         
        File[] content = source.listFiles();           
        for(int i = 0; i < content.length; i++) {
            System.out.println(content[i]);  
            move(source, destination);                
        }            
    }
    else if (!destination.isDirectory()){ 
        System.out.println("create folder here");
        destination.mkdir();
        File[] content = source.listFiles();           
        for(int i = 0; i < content.length; i++) {
            move(source, destination);              
        }
    }
 }
 catch(Exception ex) {   System.out.println(ex);    
 }
 finally {
    
 }
 }
 }

I tried the code in IDE File.move method with parameter StandardCopyOption.REPLACE_EXISTING works only if you have file in the destination folder.我尝试了 IDE File.move 方法中的代码,参数 StandardCopyOption.REPLACE_EXISTING 只有在目标文件夹中有文件时才有效。 otherwise use File.move the normal way.否则使用 File.move 正常方式。 I have also modified your code a little just to avoid code duplication.我还稍微修改了您的代码,以避免代码重复。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;

public class Move {
    static File source = new File("sourcefolderhere");
    static File destination = new File("destfolderhere");

    public static void move(File src, File dest) throws IOException {
        System.out.println(src.getName());
        if(isExist(src.getName()))
            Files.move(src.toPath().toAbsolutePath(), Paths.get(destination.getAbsolutePath()+File.separator+src.getName()) , StandardCopyOption.REPLACE_EXISTING);
        else
            Files.move(src.toPath().toAbsolutePath(), Paths.get(destination.getAbsolutePath()+File.separator+src.getName()));
    }
    
    public static boolean isExist(String souceFileName){
        //If you are not using java 8 code
        /*String[] destFiles = destination.list();
        for(String fileName : destFiles){
            if(souceFileName.equals(fileName))
                return true;
        }
        return false;*/
        return Arrays.stream(destination.list())
                .anyMatch(fileName -> fileName.equals(souceFileName));
    }

    public static void main(String[] args) throws IOException {

        try {
            if(!source.isDirectory())
                throw new IllegalArgumentException("Source Folder doesn't Exist");
            if(!destination.exists())
                destination.mkdir();
            if (source.isDirectory() && destination.isDirectory()) {
                File[] content = source.listFiles();
                for (int i = 0; i < content.length; i++) {
                    System.out.println(content[i]);
                    move(content[i], destination);
                }
            }
        } catch (Exception ex) {
            System.out.println(ex);
        } finally {

        }
    }
}```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM