This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 8898
167
  • 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: 8898
167
  • 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.
 
He baked a muffin that stole my car! And this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!