• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B7 Question about NIO.2

 
Greenhorn
Posts: 29
1
Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below is from K&B7 page 498



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?
 
Ranch Hand
Posts: 472
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

temp/test1 you will get:
temp - directory
test1 - file without extension inside directory temp

and this for directory

if your source is file you will get file, if you source is directory you will get directory
because source or file or directory result will be predictable
 
Chris Heinz
Greenhorn
Posts: 29
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply, Sergej. You are correct, that the example that I provided does not specify whether "source" was created with createFile() or createDirectories(), so it's possible that the temp/test1 is a temp directory with a test1 file that has no file extension. In all other examples in the book, the authors use the convention of directories have no file extension(obviously) and files do have a file extension. The fact that this convention was not followed in this example is what confused me. Thanks for taking the time to clear this up. Have great new year!
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


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.
 
Chris Heinz
Greenhorn
Posts: 29
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne! Thanks for replying and clearing this up. Makes much more sense now. Have a great new year!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic