Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the default root directory in Java

Tags:

java

browser

file

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.

like image 727
Ky. Avatar asked Dec 06 '10 02:12

Ky.


People also ask

How do I get to root directory in Java?

System. getProperty("user. dir"); For the last snippet, you could get the root directory by navigating upward using getParent() until null is returned.

How do I find my root directory?

Type “echo %SYSTEMROOT%" at the command prompt and press “Enter.” The result of this search is the root folder for Microsoft Windows.

What is the default directory in Java?

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.


1 Answers

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.

like image 95
btiernay Avatar answered Oct 31 '22 16:10

btiernay