0

I am working on a java project that needs to copy a directory which contains directories of images. Since I'm new to Java, I designed the below code myself to copy the directory. But I get a 'null pointer exception' error. Can someone help me correct my script? Or give me some suggestions?

public CopyDir() throws IOException {
    String destFolder1 = System.getProperty("user.home")+"/desktop/pics";
    File srcFolder = new File("C:\\rkm_vidyapith\\pics");
    if (!Files.exists(Paths.get(destFolder1),null))
        new File(destFolder1).mkdirs();
    if (srcFolder.isDirectory()) {
        for (String DirList : srcFolder.list()) {
            File FileList = new File(DirList);
            for (String EachFile:FileList.list())
                Files.copy(Paths.get(EachFile),
                           Paths.get(destFolder1),
                           StandardCopyOption.REPLACE_EXISTING);
        }
    }
}
Martin Nyolt
  • 4,463
  • 3
  • 28
  • 36
Kumar
  • 110
  • 14
  • id think you destination folder can't be found. try with `\\ ` instead of `/` – XtremeBaumer Dec 19 '16 at 08:50
  • 2
    I would use FileUtils from apache common, **https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html** – kimy82 Dec 19 '16 at 08:51
  • may be user.home variable is not passed correctly. – Manan Sheth Dec 19 '16 at 09:00
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – David Rawson Dec 19 '16 at 09:14
  • `System.getProperty("user.home")+"/desktop/pics";` This is dependent on file system. You should use the Path-Building tools of Path and Paths instead of using a specific hardcoded path separator. – Fildor Dec 19 '16 at 09:26

1 Answers1

1

I think that you code has an error !Files.exists(Paths.get(destFolder1),null) property null isn't ok at that point. I think there is better way to do that.

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

    public class FileCopy {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException{
        File sourceDir = new File("c:/tmp/pics");
        if (sourceDir.exists()) { // if we have to copy something

            File destDir = new File(System.getProperty("user.home") + "/desktop/pics");
            destDir.mkdirs(); // ensure that we do have output directory

            // do copy with nio lib
            Path destPath = destDir.toPath();
            doCopy(sourceDir, destPath);
        } else {
            System.out.println("Source directory doesn't exists!");
        }

    }

    /**
     * Do copy.
     *
     * @param sourceDir the source dir
     * @param destPath the dest path
     * @throws IOException Signals that an I/O exception has occurred.
     */
    private static void doCopy(File sourceDir, Path destPath) throws IOException{
        for (File sourceFile : sourceDir.listFiles()) {
            if (sourceFile.isDirectory()){
                File dPath = destPath.resolve(sourceFile.getName()).toFile();
                if (!dPath.exists()){
                    dPath.mkdir();
                }
                doCopy(sourceFile, dPath.toPath());
            }
            else{
                Path sourcePath = sourceFile.toPath();
                Files.copy(sourcePath, destPath.resolve(sourcePath.getFileName()));
            }
        }
    }
}
Saulius Next
  • 1,340
  • 10
  • 16
  • 1
    Your Code works but my source dir has its contents like 'pics-->pics_2016-->Images.jpeg'. But when copied the images are in the 'pics' directory instead. Help me with that please. – Kumar Dec 19 '16 at 13:46
  • I have only 6 reputations or else I would have voted for your answer. :) – Kumar Dec 19 '16 at 14:07
  • I have added recursion to do sub folder copy ( now should be able to copy this structure 'pics-->pics_2016-->Images.jpeg'). – Saulius Next Dec 19 '16 at 14:53
  • Thank You Mr. Saulius. Your code snippet worked perfect. – Kumar Dec 19 '16 at 15:12
  • Mr. Saulius could you please take a look at my another question as well. This is the link to that question. http://stackoverflow.com/questions/40924717/freetts-is-not-working-with-external-speakers – Kumar Dec 19 '16 at 15:36