How can I get the user directory of a computer in Java? Not the current working directory

StackOverflow https://stackoverflow.com/questions/23121860

  •  05-07-2023
  •  | 
  •  

Question

I'm trying to get the current user directory, where all of the users on the computer are saved, so I can save some details to this folder so it will apply to the whole computer instead of just the user area of the current user.

For example

System.getProperty("user.home")

returns C:\Users\CURRENT_USER

How can I get the "C:\Users"

Probably using substring or something using the index of the last slash? But help would be appreciated, can't seem to get it

Was it helpful?

Solution

You could find the index of the system property "user.name" in the home directory that you already have. You would use lastIndexOf to get the position, and substring to effectively remove the user name. Then you can remove the resultant trailing backslash.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top