开发者

Getting the default root directory in Java

开发者 https://www.devze.com 2023-01-29 13:27 出处:网络
I\'m making a basic file browser, and want to know how to get the default root directory. I know that java.io.File.listRoots(开发者_JAVA百科) gives all the roots (for me it\'s A:\\, C:\\, D:\\, E:\\,

I'm making a basic file browser, and want to know how to get the default root directory. I know that java.io.File.listRoots(开发者_JAVA百科) gives all the roots (for me it's A:\, C:\, D:\, E:\, F:\, G:\, H:\, I:\, L:\ T:\, U:\, X:\, Y:\, Z:\), but I want the one the user uses primarily (i.e. the one with the Operating system on it) so I know from where to start the browsing.


Not sure if this is of any help, but you could try:

import javax.swing.filechooser.*;

FileSystemView.getFileSystemView().getRoots()[0];

or

FileSystemView.getFileSystemView().getHomeDirectory();

or

System.getProperty("user.dir");

For the last snippet, you could get the root directory by navigating upward using getParent() until null is returned.


Getting the operating system root partition is only a thing on Windows since on Unix it's always /.

Hence, the following code works for Windows only:

System.getenv("SystemDrive");

It gets the SystemDrive environment variable value. This should always return the operating system's root partition e.g. C:.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号