Getting the default root directory in Java

后端 未结 2 602
甜味超标
甜味超标 2021-02-19 17:55

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

2条回答
  •  孤街浪徒
    2021-02-19 18:24

    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.

提交回复
热议问题