• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Files

 
Tony kunds
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tony kunds
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic