What is the result?

Given the code fragment:
<code>
Path path1 = Paths.get(“D:\\sales\\.\\quarterly\\..\\report”);
path1 = path1.normalize();
Path path2 = path1.relativize(Paths.get(“d:\\empdetails.dat”));
path2 = path2.resolve(path1);
System.out.println(path1);
System.out.println(path2);
}
</code>
What is the result?

Given the code fragment:

Path path1 = Paths.get("D:\\sales\\.\\quarterly\\..\\report");
path1 = path1.normalize();
Path path2 = path1.relativize(Paths.get("d:\\empdetails.dat"));
path2 = path2.resolve(path1);
System.out.println(path1);
System.out.println(path2);
}

What is the result?

A.
D: \sales\report

B.
\sales\report

C.
D: \sales\quarterly\ . . . \report

D.
\sales\report

E.
D: \sales\quarterly\ . . .\report

F.
\sales\report\empdetails.dat

G.
D: \sales\report

H.
\sales\report\empdetails.dat

Explanation:
Path1 is the normalized result of D:\\sales\\.\\quarterly\\..\\report
namely D: \sales\report.
The normalize method removes any redundant elements, which includes any “.” or “directory/..”
occurrences.
Consider path2.
With the relativize line path2 is set to../../empdetails.dat
In this scenario the following applies to the resolve statement: Passing an absolute path to
the resolve method returns the passed-in path.So Path2 will be set to Path1 in the statementpath2
= path2.resolve(path1);
Note:
A common requirement when you are writing file I/O code is the capability to construct a path from
one location in the file system to another location. You can meet this using the relativizemethod.
This method constructs a path originating from the original path and ending at the location
specified by the passed-in path. The new path is relative to the original path.
You can combine paths by using the resolve method. You pass in a partial path , which is a path
that does not include a root element, and that partial path is appended to the original path.
Reference: The Java Tutorials,Path Operations



Leave a Reply 1

Your email address will not be published. Required fields are marked *


zzZZzzzzZZzzz

zzZZzzzzZZzzz

A,G