title: Move files from parent to subdirectory using java
body: <p>Hi I am trying to copy the files and folders from root folder to subfolders which are upto level 5.It has to work same if the folders or files need to added.</p>
  <p>I have tried some examples regarding level parametric and able to list directories and Suggest me the approach to get the output folder,files structure.</p>
  <pre><code>public static void main(String[] args) throws IOException {
  
       String path = &quot;Workspace/MainFolder/&quot;;
      String targetPath=&quot;/Workspace/Dest/&quot;;
      Files.walk(Paths.get(path),5).forEach(p-&gt;{
          if(p.toFile().isDirectory()){
              Stream.of(new File(path).listFiles()).filter(File::isFile).map(f-&gt;new File(f.getPath().replace(path,targetPath))).forEach(f-&gt;{
                  f.getParentFile().mkdirs();
                  try {
                      Files.copy(p,f.toPath(),StandardCopyOption.REPLACE_EXISTING);
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              });
          }else{
              try {
                  Files.copy(p,new File(p.toFile().getPath().replace(path,targetPath)).toPath(),StandardCopyOption.REPLACE_EXISTING);
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      });
    }
   }
  </code></pre>