title: How to copy the given files only and not all the in the directory
body: <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>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&lt;String&gt; myFiles = new ArrayList&lt;String&gt;();
          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&lt;String&gt; 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&lt;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)) &gt; 0) {
                      out.write(buf, 0, len);
                  }
                  in.close();
                  out.close();
                  //System.out.println("File copied from " + sourceLocation + " to " + targetLocation);
              }
          }
      }
  </code></pre>
  
  <p>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 ?</p>