Can any one explain me the difference between Absoute,Relative,and canonical Paths & files getAbsolutePath(); getCanonicalPath(); Can some one explain with an example. Thanks in advance.
Hi corey, Thanks for helping me out here.I got from 0% to 50% in File mehtods. i have few more questions. please help me 1)when i change file to File f = new File("\test.txt"); why is there an excetion for getConocicalPath()? what exactly Cononical mean? when i change it to File f = new File("\\test.txt"); there is no exception.Why?
2)when will isFile true. File f = new File("test.txt"); f.isFile returning false. 3)when will isAbsolute() true. for every instance of file i am getting it to be false. even if i create File f = new File("c:\test.txt"); what does Absolute mean. sorry for these questions.but I am confused. Thanks
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
posted
0
Originally posted by Tony kunds: 1)when i change file to File f = new File("\test.txt"); why is there an excetion for getConocicalPath()? what exactly Cononical mean? when i change it to File f = new File("\\test.txt"); there is no exception.Why?
First of all, the term canonical means "in the simplest manner" or "in the usual way." The canonical way to refer to a file would be as C:\test.txt, not C:\temp\..\test.txt. Each File object has a single canonical form. You can check out the API Spec for more details, if you want them. Just look at the desciption for getCanonicalPath(). While you're looking at the API spec for that method, take a look at section where it talks about what this method can throw. That may very well answer your other question.
2)when will isFile true. File f = new File("test.txt"); f.isFile returning false.
From the API Spec:
Returns: true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise
Does the file exist? If not, create it and then see what this method returns.
3)when will isAbsolute() true. for every instance of file i am getting it to be false. even if i create File f = new File("c:\test.txt"); what does Absolute mean.
Absolute means the fully qualified path to the file/directory, beginning with the volume. Again, from the API Spec:
The definition of absolute pathname is system dependent. On UNIX systems, a pathname is absolute if its prefix is "/". On Win32 systems, a pathname is absolute if its prefix is a drive specifier followed by "\\", or if its prefix is "\\".
If you were to modify the code to look like this:
You should find that the isAbsolute method returns true. I hope all that helps, Corey
For f4, because of the escape character '\' escaping the following character, thus we'll have a prefix of "\\test.txt", thus f4 prints out true. However, f1 also prints true, even though there's an escape character escaping the following character, giving C:\test.txt. Shouldn't it print false out then? Why does f5 print true when the character is not '\'? Clement
though this a old post, can anyone explain me , how exactly the escape character is working here and why the output is different in the above Clement's example. thanks!!
please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details