簡體   English   中英

java.nio.file.InvalidPathException 當路徑包含冒號 (:)

[英]java.nio.file.InvalidPathException when the path contains colon (:)

我需要構建一個包含冒號 (:) 的路徑。 實際上,路徑不是 windows/linux 中的文件夾/目錄。 這只是 JCR repo 的一個路徑。 當路徑包含冒號時,下面的代碼給出 InvalidPathException

import java.nio.file.Path;
import java.nio.file.Paths;

public class TestCLass {

    public static void main(String[] args) {
        final Path path = Paths.get("com", "repo:access","resource");
        //final Path path = Paths.get("com", "repoaccess","resource"); //Output - com/repoaccess/resource
        System.out.println(path);
    }
}

例外

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 8: com\repo:access\resource
    at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
    at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
    at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
    at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
    at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
    at java.nio.file.Paths.get(Paths.java:84)

是否有任何 API 可以訪問冒號 (:) 和任何其他特殊字符?

據我所知,Windows 和 Linux 都不允許在文件名中添加“:”。 這就是 Java 為這樣的路徑/文件名拋出InvalidPathException的原因。 沒有 API 可以解決文件系統強加的限制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM