File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Files" Watch "Files" New topic
Author

Files

Tony kunds
Greenhorn

Joined: Apr 25, 2002
Posts: 25
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.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Run this and see if you still have questions.

I hope this helps, but if you have more questions, please ask and I'll elaborate.
Corey


SCJP Tipline, etc.
Tony kunds
Greenhorn

Joined: Apr 25, 2002
Posts: 25
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
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
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Hi,

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


best regards,<br />Steven<br />SCJP, SCEA
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
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
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
srinivas,
you may find the following JLS sections helpful:
JLS 3.10.6 Escape Sequences for Character and String Literals
JLS 3.3 Unicode Escapes


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
wood burning stoves
 
subject: Files