简体   繁体   中英

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.

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 . 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 . 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);
    }
}
[...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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