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()
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.
System. getProperty("user. dir"); For the last snippet, you could get the root directory by navigating upward using getParent() until null is returned.
Type “echo %SYSTEMROOT%" at the command prompt and press “Enter.” The result of this search is the root folder for Microsoft Windows.
If you start your Java app from the IDE, the current directory is usually the project root, but this can usually be configured in the launch configuration.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With