相关文章推荐
活泼的灌汤包  ·  java ...·  3 周前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Can anyone tell me what I've done wrong with the following code. I receive no errors - it just goes straight to the catch.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
    public static void main(String[] args) {
         Path source = Paths.get("C:\\Users\\Public\\Pictures\\SamplePictures");
    Path nwdir = Paths.get("D:\\NetbeansProjects\\CopyingFiles\\copiedImages");
    Files.copy(source, nwdir);
    }catch (IOException e){
        System.out.println("Unsucessful. What a surprise!");
                If you print your stacktrace, you might have a better clue as to what is going on. Add e.printStackTrace(System.out) into your catch block.
– Matthew Farwell
                Apr 12, 2012 at 15:40
                Have you tried, for example, e.printStackTrace() ? You'll get a better description of the problem.
– Ernest Friedman-Hill
                Apr 12, 2012 at 15:41

If you take a look at the Javadocs of Files.copy, you'll notice this line (emphasis added):

If the file is a directory then it creates an empty directory in the target location (entries in the directory are not copied). This method can be used with the walkFileTree method to copy a directory and all entries in the directory, or an entire file-tree where required.

So it looks like you need to use that walkFileTree method.

(And as the commenters said, print out exceptions and they'll often tell you what's wrong!)

java.nio.file.NoSuchFileException: C:\Users\Public\Pictures\SamplePictures at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) at sun.nio.fs.WindowsFileCopy.copy(WindowsFileCopy.java:99) at sun.nio.fs.WindowsFileSystemProvider.copy(WindowsFileSystemProvider.java:277) at java.nio.file.Files.copy(Files.java:1219) at Main.main(Main.java:14) – user1311422 Apr 12, 2012 at 16:46 That's nice, but, as I said, your code will not work because you're trying to do a recursive copy with a function that cannot do recursive copies. Take a look at the API link that I provided. – Jon7 Apr 12, 2012 at 16:53 By recursive copy, do you mean the directory and the files and directories within samplepictures. If so - I have changed the path to link directly to a single .jpg file with the same error. Apologies if im missing your point. – user1311422 Apr 12, 2012 at 17:12 The directory "Sample pictures" had a space in it. I thought java would complain because of the spaces, so I renamed it "SamplePictures". However, this didn't update the path. Another problem that occurred was my nwdir. I thought the path would be where I wanted the file. This is correct, but it also needs the file name. – user1311422 Apr 12, 2012 at 17:40

Came across here looking for a NIO Java7 approach to recursively copy a directory to another location. This can be done with Files.walkFileTree as Jon7 mentioned in the other anwer. This code I got for a simple directory copy:

final Path srcDir, final Path dstDir;
Files.walkFileTree(srcDir, new SimpleFileVisitor<Path>() {
    public FileVisitResult visitFile( Path file, BasicFileAttributes attrs ) throws IOException {
        return copy(file);
    public FileVisitResult preVisitDirectory( Path dir, BasicFileAttributes attrs ) throws IOException {
        return copy(dir);
    private FileVisitResult copy( Path fileOrDir ) throws IOException {
        Files.copy( fileOrDir, dstDir.resolve( srcDir.relativize( fileOrDir ) ) );
        return FileVisitResult.CONTINUE;

For a more detailed example which also handles file attributes and overwriting of existing files, see http://docs.oracle.com/javase/tutorial/essential/io/examples/Copy.java .

Beware that srcDir.relativize( fileOrDir ) will throw a ProviderMismatchException if you use this to copy across different file systems, for example, from a ZipFile to default. – Dilum Ranatunga Jun 30, 2014 at 8:15

This is how I have managed to copy a file from one location to another:

import java.io.IOException;
import static java.nio.file.StandardCopyOption.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class App  {
 public static void main(String[] args)
    Path source = Paths.get("E:/myFile.pdf");
    Path nwdir = Paths.get("F:");
       Files.copy(source, nwdir.resolve(source.getFileName()), REPLACE_EXISTING);
       System.out.println("File Copied");
    catch(IOException e)
        e.printStackTrace();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.