我一直在寻找一种方法来获取在 Windows 上找到文件的驱动器(例如 C://、E://),但我能想到的最好方法是
.getAbsolutePath().getParent().getParent() etc.
我想要的只是第一个字母,例如E:。
实现这一目标的最佳方法是什么?
最佳答案
从java 7开始使用path类,getRoot()方法文档是here
Returns the root component of this path as a Path object, or null if this path does not have a root component.
示例:
String dllPathString = "C:\\Program Files\\Notepad++\\scilexer.dll";
Path dllPath = Paths.get(dllPathString);
Path driveLetter = dllPath.getRoot();
System.out.println("The dll is in: " + driveLetter);
关于java - Java中获取文件所在驱动器的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42746482/