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
  •  | 
  •  

문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top