• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Paths Path Java NIO J7 Question from G&S Book

 
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all,

I am still reading the G&S book thanks to enough free time, working computer, and relatively good health. Some people think C and Python are the best languages but I am continuing to venture down this binary Java programming road of mystery and wonder. Without further ado, could someone please explain to me how the normalize() method from the Path class is working in this example found on page 279 of the G&S book on passing the OCP Java 7 SE Programmer 2 exam?

Here is teh codez:



Thank-you for reading Java Ranch dot com. I hope everyone is having an awesome time and living out their wildest dreams everyday.

Sincerely,

Ted
 
author & internet detective
Posts: 41086
848
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can think of the normalize() method as creating the shortest possible representation of the path. (This isn't strictly true as it doesn't take into account symbolic links, but it is a good way to remember that is going on.)

Normalize condenses . (current directory) by getting rid of it. It also condenses .. (parent directory) by removing the directory immediately before it.

In this case, there are two substitutions.

Start: D:\\OCPJP7\\programs\\..\\NIO2\\src\\.\\SubPath.java
Start: D:\\OCPJP7\\NIO2\\src\\.\\SubPath.java - get rid of programs because of .. (this works because there is no reason to drill down into the programs directory only to immediately to go the parent directory - also know as D:\\OCPJP7 - the directory you started in)
Start: D:\\OCPJP7\\\\NIO2\\src\\SubPath.java - just get rid of \\. as it is redundant
 
Ted North
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne,

Thank-you for the helpful reply. I think I understand that the single dot is removed - just because it does not make a lot of sense in a path to a file but the two dots are confusing still. 0_o It is sort of surprising that the Path compiles at all with the strange dots in the middle of the URL or URI.

Do the two dots mean to move back one directory to the previous directory, which is just above the 'programs' directory? So since the path is going into the 'programs' directory then directly out of it with the double periods the normalize() method removes it?

For instance in a BASH shell typing: cd ../ will move back one directory. Where as cd <directory-name> moves to the typed directory.

This is even stranger because I rarely use Windows any more and the question is with a Windows file system.

I hope this made sense to someone other than me.

Sincerely,

Ted
 
Jeanne Boyarsky
author & internet detective
Posts: 41086
848
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ted North wrote:Do the two dots mean to move back one directory to the previous directory, which is just above the 'programs' directory? So since the path is going into the 'programs' directory then directly out of it with the double periods the normalize() method removes it?


Exactly.

Ted North wrote:For instance in a BASH shell typing: cd ../ will move back one directory. Where as cd <directory-name> moves to the typed directory.

This is even stranger because I rarely use Windows any more and the question is with a Windows file system.


Stick with that mental model. cd .. works on Windows too. Really, it does. Java tries to be platform independent so you can switch the directory path separators. Which means I could write c:/OCPJP7 and Java will be happy as a clam.
 
Ted North
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:

Ted North wrote:Do the two dots mean to move back one directory to the previous directory, which is just above the 'programs' directory? So since the path is going into the 'programs' directory then directly out of it with the double periods the normalize() method removes it?


Exactly.

Ted North wrote:For instance in a BASH shell typing: cd ../ will move back one directory. Where as cd <directory-name> moves to the typed directory.

This is even stranger because I rarely use Windows any more and the question is with a Windows file system.


Stick with that mental model. cd .. works on Windows too. Really, it does. Java tries to be platform independent so you can switch the directory path separators. Which means I could write c:/OCPJP7 and Java will be happy as a clam.



Cool. Thank-you Jeanne for helping me to understand this normalize() method. I am glad Java is platform independent. I can write once and run it everywhere.

P.S. - That was interesting to read about cd on Windows. Working with the operating-system (OS) in a shell is often times more fun than a GUI like Explorer on Windows or Nautilus on Ubuntu.

Sincerely,

Ted
 
Grow your own food... or this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic