How to create an automatic path in a Java application

I am creating a small application to take a screenshot. I can save it automatically on my desktop using this path:

String FILENAME = "C:\\Users\\obed\\Desktop\\" + x + ".png"; ImageIO.write(image, "png", new File(FILENAME)); 

where Obed is my username. How can I automatically change β€œobed” to a username so that I can run my application on any computer this way?

+5
source share
1 answer

You can get the user's home directory from the user.home system property:

 String FILENAME = System.getProperty("user.home") + "\\Desktop\\" + x + ".png"; 
+5
source

Source: https://habr.com/ru/post/1205638/


All Articles