将以 \\?\ 开头的设备路径转换为 Path
Convert device path starting with \\?\ to Path
我想在 java 程序中读取和写入数据到 HID 设备。
Windows 中的设备路径格式如下:
\?\HID#VID_XXXX&PID_XXXX#...#{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
。 \?\
前缀是 special windows namespace.
我可以使用 RandomAccessFile
打开设备没有问题,因为构造函数需要 String
.
但是,当我尝试将其转换为 Path
时,会发生这种情况:
Path.of("\\?\HID#VID_XXXX&PID_XXXX#...#{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}");
Exception in thread "main" java.nio.file.InvalidPathException: Illegal character [?] in path at index 2: \?\HID#VID_XXXX&PID_XXXX#...#{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
at java.base/sun.nio.fs.WindowsPathParser.nextSlash(WindowsPathParser.java:212)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:111)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:232)
at java.base/java.nio.file.Path.of(Path.java:147)
at ...
因为我想使用 AsynchronousFileChannel
class,我必须先将它转换为 Path
。有办法吗?
用 \.\
替换 \?\
似乎可以解决问题:
String rawPath = "\\?\HID#VID_XXXX&PID_XXXX#...#{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"
var newPath = Path.of(rawPath.replace("\\?\", "\\.\");
我想在 java 程序中读取和写入数据到 HID 设备。
Windows 中的设备路径格式如下:
\?\HID#VID_XXXX&PID_XXXX#...#{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
。 \?\
前缀是 special windows namespace.
我可以使用 RandomAccessFile
打开设备没有问题,因为构造函数需要 String
.
但是,当我尝试将其转换为 Path
时,会发生这种情况:
Path.of("\\?\HID#VID_XXXX&PID_XXXX#...#{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}");
Exception in thread "main" java.nio.file.InvalidPathException: Illegal character [?] in path at index 2: \?\HID#VID_XXXX&PID_XXXX#...#{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
at java.base/sun.nio.fs.WindowsPathParser.nextSlash(WindowsPathParser.java:212)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:111)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:232)
at java.base/java.nio.file.Path.of(Path.java:147)
at ...
因为我想使用 AsynchronousFileChannel
class,我必须先将它转换为 Path
。有办法吗?
用 \.\
替换 \?\
似乎可以解决问题:
String rawPath = "\\?\HID#VID_XXXX&PID_XXXX#...#{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"
var newPath = Path.of(rawPath.replace("\\?\", "\\.\");