2

I currently have the following code:

ImageIO.write(imageBlue, "PNG", new File("c://imageBlue.PNG"));

But I want the program to write it to my Desktop, no matter when Directory I am currently in.

2
  • 1
    Just use a absolute path to the desktop. Commented Mar 28, 2016 at 14:10
  • Can you use %userprofile%\\Desktop (when it is windows). Else there is probably a class that will is dependend determine the user home directory Commented Mar 28, 2016 at 14:11

3 Answers 3

6

You can use System.getProperty(String) to get the user.home System Property. Then, use that to get the Desktop. Finally, use that to get your desired output File. Something like,

String homeFldr = System.getProperty("user.home");
File desktop = new File(homeFldr, "Desktop");
ImageIO.write(imageBlue, "PNG", new File(desktop, "imageBlue.PNG"));
Sign up to request clarification or add additional context in comments.

Comments

3

Only this configuration needed,

ImageIO.write(imageBlue, "PNG", new File(System.getProperty("user.home") + "/Desktop");

Comments

1

You should change the path of to following :

C:\Users\{your-user-id}\Desktop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.