在我的Windows机器上,我的主硬盘驱动器上有字母C:和名称"Local disk".
要在Windows上列出Java中的驱动器号,File对象具有静态listRoots()方法.但我无法找到一种方法来获取Windows上的驱动器名称(而不是驱动器号).
有人曾尝试过这个吗?
小智 11
是的,您需要获取FileSystemView对象并使用getSystemDisplayName.(我曾经用Java实现了一个Filesystem浏览器).
虽然它并不完美但它会让你得名.从文档:
将在系统文件浏览器中显示的文件,目录或文件夹的名称.Windows中的示例:"M:\"目录显示为"CD-ROM(M :)"默认实现从ShellFolder类获取信息.
Rea*_*wTo 10
实际上,要获取驱动器名称(例如本地磁盘),您需要使用getSystemTypeDescription.getSystemDisplayName返回卷名.
import java.io.File;
import java.util.Arrays;
import java.util.List;
import javax.swing.filechooser.FileSystemView;
public class Test2 {
public static void main(String args[]){
List <File>files = Arrays.asList(File.listRoots());
for (File f : files) {
String s1 = FileSystemView.getFileSystemView().getSystemDisplayName (f);
String s2 = FileSystemView.getFileSystemView().getSystemTypeDescription(f);
System.out.println("getSystemDisplayName : " + s1);
System.out.println("getSystemTypeDescription : " + s2);
}
/* output (French WinXP)
getSystemDisplayName :
getSystemTypeDescription : Disquette 3½ pouces
getSystemDisplayName : REGA1 (C:)
getSystemTypeDescription : Disque local
getSystemDisplayName :
getSystemTypeDescription : Lecteur CD
getSystemDisplayName : My Book (F:)
getSystemTypeDescription : Disque local
*/
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15509 次 |
最近记录: |