How to save a image using ImageIO.write() in Linux?

1.4k Views Asked by At

I wanted to save image to user machine while he is launching my java application. for this i have written code as follows :

BufferedImage image = null;
        try {

            URL url = new URL(logourl);
            image = ImageIO.read(url);

            ImageIO.write(image, "png",new File("/usr/swa.png"));

        } catch (IOException e) {
            e.printStackTrace();
        }

But it is giving me

java.io.FileNotFoundException: /usr/swa.png (Permission denied)

but if i used

ImageIO.write(image, "png",new File("/home/MyUserName/Desktop/applicationName/logo.png"));

Then it saving the image. i dont get why it is not accessing that path if it working for /home/MyUserName/Desktop/applicationName/logo.png this path.

basically when user install application i want fetch image from web url and save it to user's local machine so what i need to to achieve this??

EDIT : Exception corrected

1

There are 1 best solutions below

0
user207421 On

You don't have write permission to /usr, or, less probably, the file /usr/swa.png already exists and isn't deletable or writable by you.