What is the result?

Given:

import java.io.IOException;

import java.io.file.Path;

import java.io.file.Paths;

public class Path12 {

public static void main(String s[]) throws IOException {

Path path = Paths.get(“\\sales\\quarter\\..\\qtrlreport.txt”);

path.relativize(Paths.get(“\\sales\\annualreport.txt”));

if(path.endsWith(“annualreport.txt”)) {

System.out.println(true);

} else {

System.out.println(false);

}

System.out.println(path);

}

}

What is the result?

Given:

import java.io.IOException;

import java.io.file.Path;

import java.io.file.Paths;

public class Path12 {

public static void main(String s[]) throws IOException {

Path path = Paths.get(“\\sales\\quarter\\..\\qtrlreport.txt”);

path.relativize(Paths.get(“\\sales\\annualreport.txt”));

if(path.endsWith(“annualreport.txt”)) {

System.out.println(true);

} else {

System.out.println(false);

}

System.out.println(path);

}

}

What is the result?

A.
false
\sales\quarter\ . . \qtrlreport.txt

B.
false
\quarter\ . . \qtrlreport.txt

C.
true
. . \ . . \ . . \ annualreport.txt

D.
true
\ . . \ . . \annualreport.txt

Explanation:
The relativize method that can be used to construct a relative path between two paths.

relativize
Path relativize(Path other)
Constructs a relative path between this path and a given path.
Parameters:
other – the path to relativize against this path
Returns:
the resulting relative path, or an empty path if both paths are equal

Note:
Relativization is the inverse of resolution. This method attempts to construct a relative path that when resolved against this path, yields a path that locates the same file as the given path. For example, on UNIX, if this path is “/a/b” and the given path is “/a/b/c/d” then the resulting relative path would be “c/d”. Where this path and the given path do not have a root component, then a relative path can be constructed. A relative path cannot be constructed if only one of the paths have a root component. Where both paths have a root component then it is implementation dependent if a relative path can be constructed. If this path and the given path are equal then an empty path is returned.

For any two normalized paths p and q, where q does not have a root component,

p.relativize(p.resolve(q)).equals(q)
When symbolic links are supported, then whether the resulting path, when resolved against this path, yields a path that can be used to locate the same file as other is implementation dependent. For example, if this path is “/a/b” and the given path is “/a/x” then the resulting relative path may be “../x”. If “b” is a symbolic link then is implementation dependent if “a/b/../x” would locate the same file as “/a/x”.



Leave a Reply 2

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


Charlie

Charlie

The return value of path.relativize() is not assigned to path, so path still has the original value.

Jav

Jav

A
false
\sales\quarter\ . . \qtrlreport.txt