JAVA path.getName(0) returning the entire path instead of first element on a Mac, with windows path

huangapple go评论70阅读模式
英文:

JAVA path.getName(0) returning the entire path instead of first element on a Mac, with windows path

问题

I suspect this is a noob mistake on my behalf. With Java 13 and the following method:

public static void winPath() {
    Path winPath = Paths.get("C:\\the\\wizards\\despicable\\cat");
    System.out.println(String.format("First element of %s is: %s", winPath.toString(), winPath.getName(0)));
}

Calling this method, I would expect to get:

First element of C:\the\wizards\despicable\cat is: the

Instead, I get the entire path:

First element of C:\the\wizards\despicable\cat is: C:\the\wizards\despicable\cat

This is unexpected behavior to me because if I try the same with a macOS path:

public static void macPath() {
    Path macpath = Paths.get("/Volumes/Multimedia/the/wizards/despicable/cat");
    System.out.println(String.format("First element of %s is: %s", macpath.toString(), macpath.getName(0)));
}

... the output is as I hoped for:

First element of /Volumes/Multimedia/the/wizards/despicable/cat is: Volumes

Any help would be appreciated!

英文:

I suspect this is a noob mistake on my behalf. With Java 13 and the following method:

    public static void winPath (){
        Path winPath = Paths.get("C:\\the\\wizards\\despicable\\cat");
        System.out.println(String.format("First element of %s is: %s", winPath.toString(), winPath.getName(0)));
    }

Calling this method, I would expect to get:

First element of C:\the\wizards\despicable\cat is: the

Instead I get the entire path:

First element of C:\the\wizards\despicable\cat is: C:\the\wizards\despicable\cat

This is unexpected behaviour to me, because if I try the same with a macos path:

 public static void macPath (){
        Path macpath = Paths.get("/Volumes/Multimedia/the/wizards/despicable/cat");
        System.out.println(String.format("First element of %s is: %s", macpath.toString(), macpath.getName(0)));
    }

... the output is as I hoped for:

First element of /Volumes/Multimedia/the/wizards/despicable/cat is: Volumes

Any help would be appreciated!

答案1

得分: 1

Path 在非 Windows 系统上执行时不会将字符串分成不同的元素,因为它无法识别文件分隔符。要创建一个每个磁盘/文件夹/文件都是不同元素的路径,你需要这样创建:

Path winPath = Paths.get("C:", "\\the", "\\wizards", "\\despicable", "\\cat");

或者更好的做法,因为你不想包含 \

Path winPath = Paths.get("C:", "the", "wizards", "despicable", "cat");

然后,你可以遍历这些元素:

winPath.forEach(p -> System.out.println(p));

这就是为什么在 Mac(或 Linux/Unix)上运行时,你的第二个示例按预期工作:

Paths.get("/Volumes/Multimedia/the/wizards/despicable/cat");

将给定的路径分割为不同的元素,例如 "Volume"、"Multimedia" 等。

英文:

Path will not divide your string into different elements when you execute this on a non Windows system because it doesn't recognise the file separator, so to create a path where each disk/folder/file is different element you need to create it like this

Path winPath = Paths.get("C:", "\\the", "\\wizards", "\\despicable", "\\cat");

or even better since you don't want the \ included

Path winPath = Paths.get("C:", "the", "wizards", "despicable", "cat");

Then you can iterate over your elements

winPath.forEach( p ->
    System.out.println(p)
);

This is why your second example works as expected when run on a Mac (or a Linux/Unix) machine

Paths.get("/Volumes/Multimedia/the/wizards/despicable/cat");

will split the given path into different elements, "Volume", "Multimedia" and so on

huangapple
  • 本文由 发表于 2020年8月1日 14:17:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63202385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定