2

There is a default (FileSystem?) location in Java.

E.g. when you instantiate a JFileChooser without specifying what folder to open in, it will open in that default location.

I need to get that default location as a Path object (without use of the JFileChooser, that way just to explain).

How can I get it?

Karlovsky120
  • 6,212
  • 8
  • 41
  • 94
  • 1
    possible duplicate of [How do I get my current working directory in Java?](http://stackoverflow.com/questions/3153337/how-do-i-get-my-current-working-directory-in-java) – Brian Roach Jul 19 '13 at 00:16
  • I think this question duplicates http://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java – fvu Jul 19 '13 at 00:17
  • 1
    You should be able to create a `Path` from `System.getProperty("user.dir")` which is the user's home directory – MadProgrammer Jul 19 '13 at 00:20
  • 1
    @Karlovsky120 Added with a possible example, no testing, but it should get you started – MadProgrammer Jul 19 '13 at 00:49

2 Answers2

3

You should be able to create a Path from System.getProperty("user.home") which is the user's home directory

Something like...

Path path = FileSystems.getDefault().getPath(System.getProperty("user.home"));

Updated

JFileChooser uses FileSystemView to obtain it's "default" directory

 Path path = FileSystemView.getFileSystemView().getDefaultDirectory().toPath()

Equally, you could also use something like...

Path docs = FileSystems.getDefault().getPath(System.getProperty("user.home"), "Documents");
Path myDocs = FileSystems.getDefault().getPath(System.getProperty("user.home"), "My Documents");
Path userHome = FileSystems.getDefault().getPath(System.getProperty("user.home"));

And test each to see if they actually exist

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Actually, that's not what I wanted. I expected it to open my documents, instead it opens the location of the class file... I want it to open My documents... Users home directory, not on program scale, but whole opearting system scale... – Karlovsky120 Jul 19 '13 at 00:55
  • 1
    Should be `user.home`. You can look for "My Documents" to see if it exists, but remember, other OS's don't have a "My Documents" folder – MadProgrammer Jul 19 '13 at 00:59
  • Hmmm... I'm trying to synchronise this with the JFileChooser. If user opens a new document, a path to the document will be saved whichever it may be. However, if user creates a new document from scratch, I need to save some kind of path to the document, which would be the default JFileChooser path. How does JFileChooser get it's default source? – Karlovsky120 Jul 19 '13 at 01:03
  • 1
    And that is exactly what I needed. I could manually get to My documents, but I wanted it to be platform independant. =) – Karlovsky120 Jul 19 '13 at 01:10
1

Not sure if this is what you are looking for... For JFileChooser, the default directory is typically the "My Documents" folder on Windows, and the user's home directory on Unix. Source.

If you want the working directory's path, then CurrentClass.class.getProtectionDomain().getCodeSource().getLocation().getPath().

  • *"CurrentClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()"* - Wouldn't `System.getProperty("user.dir")` return the same thing? – MadProgrammer Jul 19 '13 at 01:15