简体   繁体   English

如何仅复制给定的文件而不是目录中的所有文件

[英]How to copy the given files only and not all the in the directory

I am trying to create a customized file copier that it takes, source folder, destination folder and an array list containing some file names. 我试图创建一个需要的自定义文件复印机,源文件夹,目标文件夹以及包含一些文件名的数组列表。 Then the copier copies the files in the ArrayList, with the same structure. 然后,复印机将文件复制到具有相同结构的ArrayList中。

I could achieve it by copying only if the file name is the same as a element in the array. 仅当文件名与数组中的元素相同时,我才能通过复制来实现。 But it creates all the folders, no matter if the target file does not exist in them. 但是,无论目标文件是否不存在,它都会创建所有文件夹。

public static void main(String[] args) {
        // TODO code application logic here
        File source = new File("D:\\Documents\\A X");
        File dest = new File("D:\\Documents\\A X Sample");
        ArrayList<String> myFiles = new ArrayList<String>();
        myFiles.add("Tohi - Rooh.mp3");
        try{
            copyDirectory(source, dest, myFiles);;
        }catch(IOException e){
            e.printStackTrace();
            System.exit(0);
        }

    }
    public static void copyDirectory(File sourceLocation , File targetLocation, ArrayList<String> array)
    throws IOException {

        if (sourceLocation.isDirectory()) {
            String[] children = sourceLocation.list();
            for(String element : children){
                if (array.contains(element)){
                    File fileToCopy = new File(sourceLocation, element);
                    //System.out.println(fileToCopy.getAbsolutePath());
                    targetLocation.mkdir();
                }
                //if (!targetLocation.exists()) {
                //        targetLocation.mkdir();
                //}    
            }




            for (int i=0; i<children.length; i++) {
                copyDirectory(new File(sourceLocation, children[i]),
                        new File(targetLocation, children[i]), array);
            }
        } else {
            if(array.contains(sourceLocation.getName())){
                InputStream in = new FileInputStream(sourceLocation);
                OutputStream out = new FileOutputStream(targetLocation);

                // Copy the bits from instream to outstream
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
                //System.out.println("File copied from " + sourceLocation + " to " + targetLocation);
            }
        }
    }

So I want it to stop making useless blank folders, and only make them if they hold a target file from the array. 因此,我希望它停止制作无用的空白文件夹,并且仅当它们持有阵列中的目标文件时才进行制作。 Any idea ? 任何想法 ?

I have a method that copies a file to another location. 我有一种将文件复制到另一个位置的方法。 Maybe it helps private 也许可以帮助私人

void copyFile(File source,File destination) throws IOException
{
    InputStream is; OutputStream os;
    try
    {
        is=new FileInputStream(source);
        os=new FileOutputStream(destination);
        byte[] buffer=new byte[1024];
        int length;
        while((length=is.read(buffer))>0)
        { os.write(buffer,0,length); }
        is.close();
        os.close();
    }
    catch(NullPointerException e){}
}

And if you wanna check wether a file exists, try to read from it and it will throw an error if it doesn't exist. 如果您想检查文件是否存在,请尝试从中读取文件,如果文件不存在,则会抛出错误。

You are calling your method copyDirectory for each children of your sourceLocation . 您正在为copyDirectory的每个子级调用方法sourceLocation A way of correcting it (not the only or the best) is saving the files you want to copy and are in the sourceDiretory on a List . 纠正它的一种方法(不是唯一的还是最好的)是保存要复制的文件,并将它们放在ListsourceDiretory For example: 例如:

[...]
if (sourceLocation.isDirectory()) {
    String[] children = sourceLocation.list();
    // List to save the name of the files you want to copy
    List<String> foundFiles = new ArrayList<String>(array.size()); 
    for(String element : children){
        if (array.contains(element)){
            // If the file you want to copy are in the sourceDiretory 
            // add its name to the list
            foundFiles.add(element); 
            targetLocation.mkdirs();   
        }
    }    
    for (String foundFile : foundFiles) {
         copyDirectory(new File(sourceLocation, foundFile),
                    new File(targetLocation, foundFile), array);
    }
}
[...]

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

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