• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

java.io.File methods - big muddle!  RSS feed

 
Ranch Hand
Posts: 236
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Review Question 11.5 from Mughal/Rasmussen's book, page 485.



I take it he is using the constructor that takes a parent pathname string and a child pathname string. From the API:

public File(String parent, String child)

parent - The parent abstract pathname
child - The child pathname string


Now I don't understand the second argument passed: "../book/../chapter1"

What do the double dots mean? (jump one directory up? to where?)

What is the full directory structure of where the file was created?
 
T Vergilio
Ranch Hand
Posts: 236
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now this is interesting. I compiled and ran the code, and got the following output:

.\documents\..\book\..\chapter1
C:\Users\chandella\Documents\MyProjects\.\documents\..\book\..\chapter1
C:\Users\chandella\Documents\MyProjects\chapter1
chapter1
.\documents\..\book\..

Process completed.

So I changed the code slightly and inserted a call to mkdir, to see where the new directory would be created:



I looked through my folders, and the new directory, called chapter1, was created in:

C:\Users\chandella\Documents\MyProjects\


where is the book directory? what happened?
 
Saloon Keeper
Posts: 9145
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no book directory, because the .. means "go one directory up".

So the first .. effectively cancels documents, and the second .. cancels book, leaving just .\chapter1. This means "look for chapter in the current directory".

you should also keep in mind that creating a File instance does not actually create the directory structure. Instances of File are really just path names, whether the indicated file exists or not.

Calling the class File was a mistake, they should have called it Path instead. Indeed, Java 7 offers Path instead of File.
 
T Vergilio
Ranch Hand
Posts: 236
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see... thanks, Stephan. Not very intuitive, is it? If you don't mind me asking just one more question: why do the methods from File show the double dots then? Do File.getPath() and File.getAbsolutePath() always show the paths "as entered"? And File.getCanonicalPath() shows the path structure as it actually is?
 
Stephan van Hulst
Saloon Keeper
Posts: 9145
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the File class doesn't resolve symbolic links. What if you're working in an operating system where the .. would be a legal name for an actual directory?

This is why File always stores the path as you entered it. The getCanonicalPath() then asks the operating system to resolve this path String (which is why this method can throw an IOException), removing symbolic links and leaving you with a unique path to the actual location.
 
T Vergilio
Ranch Hand
Posts: 236
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all clear now. Cheers, Stephan.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!