0

我没有 Windows 副本,但想知道在 Java 中表示路径的行为和推荐用法,例如\autoexec.bat在 Windows 下?

从语义上讲,这样的路径将代表autoexec.bat任何文件系统根目录上的文件。C:\因此,在表示文件之前,需要根据表示磁盘驱动器的路径(例如 )来解析它。从这个意义上说,它不是绝对的。但是,我想它也没有根组件。

在 Windows 上运行 JVM 时可以创建这样的路径吗?如果是这样,会getRoot()返回isAbsolute()什么?

我使用Memory File System尝试了以下代码,但这会抛出InvalidPathException:“path must not start with separator at index 1: \truc”。这是否忠实地反映了 Windows 下的行为,还是这个特定库的怪癖?

try (FileSystem fs = MemoryFileSystemBuilder.newWindows().build()) {
    final Path truc = fs.getPath("\\truc");
    LOGGER.info("Root: {}.", truc.getRoot());
    LOGGER.info("Abs: {}.", truc.isAbsolute());
    LOGGER.info("Abs: {}.", truc.toAbsolutePath());
}

这样的路径在 Windows 终端中是有效的,或者至少是我上次使用 Windows 时(很久以前)。创建这样的路径以标记路径是“绝对的”会很方便(从反斜杠开始的意义上说,因此与文件夹无关),但仍然保留没有指定驱动程序字母的路径。然后可以(稍后)将这样的路径解析为C:\autoexec.bator D:\autoexec.bator ...</p>

4

1 回答 1

1

在 Windows 中,\\是指C:\在我的情况下的当前驱动器。

不知道如何MemoryFileSystemBuilder工作,但以下代码

File file = new File("\\test.txt");
final Path truc = file.toPath();
System.out.println("Root: " + truc.getRoot().toString());
System.out.println("Abs: " + truc.isAbsolute());
System.out.println("Abs: " + truc.toAbsolutePath().toString());

给出以下输出

Root: \
Abs: false
Abs: C:\test.txt
于 2019-08-05T03:23:29.703 回答