• 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
Ranch Hand
Posts: 238
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: 238
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?
 
Bartender
Posts: 9491
184
  • 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: 238
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
Bartender
Posts: 9491
184
  • 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: 238
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all clear now. Cheers, Stephan.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!