Path Class Resolve Method

Asked 2 years ago, Updated 2 years ago, 36 views

import java.io.*;
import java.nio.file.*;

US>class Test {
    public static void main(String[]args) rows IOException {
        Path p1 = Paths.get("C:\\gold\\.");
        p1 = p1.resolve("dir1");
        Path p2 = Paths.get ("C:\\gold");
        p2 = p2.resolve("\\dir2");
        System.out.print(p1+":");
        System.out.print(p2);
    }
}

Variable p2 is output to :C:\dir2 " but is an argument for the resolve method

"\dir2" is not an absolute path.

In my opinion, the output of variable p2 should be "C:\gold\dir2", but what is my understanding

Please let me know if it's wrong.

java windows

2022-09-30 16:59

1 Answers

https://msdn.microsoft.com/ja-jp/library/windows/desktop/aa365247(v=vs.85).aspx#paths

According to the description in ,

  • \\ (two backslashes) starting with (UNC path)
  • C:\, d:\, and so on, starting with a drive letter and slash
  • (starting with a single backslash)

is the absolute path, so \dir2 is the absolute path.

Below is the original text

Fully Qualified vs. Relative Paths

For Windows API functions that manage files, file names can be relevant to the current directory, while some APIs require a fully qualified path.A file name is relative to the current directory if does not begin with one of the following:

  • AUNC name of any format, which always start with two backslash characters("\\").For more information, see the next section.
  • A disk designer with a backslash, for example "C:\" or "d:\".
  • A single backslash, for example, "\directory" or "\file.txt".This is also referred to as an absolute path.


2022-09-30 16:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.