Results 1 to 4 of 4
  1. #1
    cnh57811 is offline Member
    Join Date
    Dec 2014
    Posts
    3
    Rep Power
    0

    Default NIO.2 Files.copy() ??

    The code below is from K&B7 page 498

    Java Code:
    Path source = Paths.get("/temp/test1");      // exists
    Path target = Paths.get("/temp/test2.txt");  // doesn't yet exist
    Files.copy(source, target);                          // now two copies of the file
    Files.delete(target);                                    // back to one copy
    Files.move(source, target);                         // still one copy
    What I don't understand is...how is the "source" a file? (Note the comment on line3 "now two copies of the FILE") From the test code that I've written, "source" is a directory and when the copy occurs, a new directory(folder) named "test2.txt" is created. Was that the intended outcome of this code snippet? What would make more sense, is if the "source" was an actual file, like "someFile.txt" and then the contents of someFile.txt would be copied into a new file called "test2.txt". But this whole idea of copying a directory into a file is a bit confusing. Can somebody shed some light on what exactly the author is trying to accomplish here?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: NIO.2 Files.copy() ??

    From the book, how do you know /temp/test1 is a directory? It could be a regular file with no extension?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    cnh57811 is offline Member
    Join Date
    Dec 2014
    Posts
    3
    Rep Power
    0

    Default Re: NIO.2 Files.copy() ??

    Quote Originally Posted by jim829 View Post
    From the book, how do you know /temp/test1 is a directory? It could be a regular file with no extension?

    Regards,
    Jim
    Thanks for your reply, Jim. You are 100% correct. I don't know if /temp/test1 is a file or a directory. I suppose I would have to know whether createFile() or createDirectories() was used. The author tells us that test1 is a file, so I guess I have to just take his statement at face value. What confused me is that in all other examples in the book, the author used a convention where directories do not have a file extension(obviously) and files DO have a file extension. I was expecting to see that convention continued in this example. Thanks for your help.

  4. #4
    cnh57811 is offline Member
    Join Date
    Dec 2014
    Posts
    3
    Rep Power
    0

    Default Re: NIO.2 Files.copy() ??

    Officially closing the loop on this. One of the technical contributors to this book saw my post over at the coderanch and replied to say that this was a typo. It will be added to the book errata. Her response is below. Thanks, Jeanne!

    Quote Originally Posted by Jeanne Boyarsky
    Chris,
    One of the parts of the book I contributed was the NIO section so I can tell you exactly what the author was trying to accomplish :).

    My test code for that example was:
    Java Code:
    Path source = Paths.get("/Users/nyjeanne/Documents/workspace/Bert-Book/examples/nio/chapter/test1.txt");
    Path target = Paths.get("/Users/nyjeanne/Documents/workspace/Bert-Book/examples/nio/chapter/test2.txt");
    		
    Files.copy(source, target);
    
    Files.delete(target);
    Files.move(source, target);
    I then made the path shorter for the purposes of the book. And managed to lose the extension of the file in doing so. In other words, human error at my end. source was definitely supposed to be a file! The example right below it in the book has the exact same problem. I've added this to the errata thread/wiki page. Thank you for pointing it out.

Similar Threads

  1. Copy Constructor with Shallow Copy
    By Wnt2bsleepin in forum New To Java
    Replies: 1
    Last Post: 04-11-2012, 12:42 AM
  2. How to copy files to system clipboard?
    By Reptar693 in forum AWT / Swing
    Replies: 1
    Last Post: 04-06-2012, 03:52 PM
  3. how to copy the files from fileserver to local path via browser?
    By mariaprabudass in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-12-2011, 04:00 PM
  4. Copy files within JAR to disk
    By RaF in forum New To Java
    Replies: 2
    Last Post: 08-31-2010, 04:25 PM
  5. Copy files between servers
    By mcoto800 in forum Java Servlet
    Replies: 1
    Last Post: 02-05-2010, 01:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •