Copying files from one directory to another in java throws NoSuchFileException -


i have hashset of strings, names of files want copy working direcorory "path" directory. find following piece of code should working, however, java.nio.file.nosuchfileexception: /commits/1/hello.txt exception.

  hashset<string> stagedqueue = hashset<string>();     stagedqueue.put("hello.txt");     stagedqueue.put("bye.txt");   string path = "/commits/" + commitid;                  (string file : stagedqueue) {                     files.copy((new file(file).topath()),                          (new file(path + "/" + file).topath())); 

what can fix this? can't figure out why getting these exceptions. please note moving these empty directory.

don't go through file; use java.nio.file.

your problem here try , copy initial file directory not exist yet:

string path = "/commits/" + commitid; 

first of all, destination directory, call dstdir, instance. create base directory , copy files it:

final path basedir = paths.get("/commits", commitid);  files.createdirectories(basedir);  (final string file: basequeue)     files.copy(paths.get(file), basedir.resolve(file)); 

Popular posts from this blog