Nicole_Maguire
Nicole_Maguire

Reputation: 63

Setting a path when creating a new File in Java

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.

Upvotes: 2

Views: 136

Answers (3)

Vishal Gajera
Vishal Gajera

Reputation: 4207

Only this configuration needed,

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

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201399

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"));

Upvotes: 6

Pranav Maniar
Pranav Maniar

Reputation: 1565

You should change the path of to following :

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

Upvotes: 1

Related Questions