0

I am trying to do the above, but each time, I keep getting the following errorjava.io.FileNotFoundException: file:\C:\Users\User\Desktop\Scrap\main\out\production\resources\ProfilePic\xas.png (The filename, directory name, or volume label syntax is incorrect)

Here's the function I used to do this

  private URL url = this.getClass().getResource("/ProfilePic");
  public final String PICTURE_DIRECTORY  = url.toString();


 public String createNewPicture (String path, String newPictureName) {
    int width = 12;
    int height = 12;
    BufferedImage bf = null;
    File f = null;
    String dst = PICTURE_DIRECTORY +"/"+newPictureName+".png";
    try{
        f = new File("F://a.jpg");
        bf = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        bf = ImageIO.read(f);
        System.out.println("read file successfully");


    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    try {
        dst = PICTURE_DIRECTORY +"/"+newPictureName+".png";
        new File (PICTURE_DIRECTORY, newPictureName+".png");
        f = new File(dst);
       ImageIO.write(bf, "png", f);
        //System.out.println("asaas " +dst);
    }
    catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return dst;
}

Can someone please help me? Spent several hours trying to solve this but stuck. Thanks!

4
  • 1
    Does the directory exists? Commented Mar 18, 2018 at 9:20
  • You don't need the line, bf = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); You just get rid of it on the next line. Which line is causing the file not found? Also you cannot write to a resource like that. Your PICTURE_DIRECTORY is wrong. Commented Mar 18, 2018 at 9:28
  • The directory doesn't exist. There are several lines of redundant code here. Commented Mar 18, 2018 at 9:30
  • ImageIO is great when you want to display or modify images. For copying files, it's simply the wrong solution. Use Files.copy(...), see the Copying a File or Directory tutorial. Commented Mar 19, 2018 at 7:58

1 Answer 1

1

The information you need is found in the error message:

java.io.FileNotFoundException: file:\C:\Users\User\Desktop\Scrap\main\out\production\resources\ProfilePic\xas.png (The filename, directory name, or volume label syntax is incorrect)

The problem in your code is found here:

private URL url = this.getClass().getResource("/ProfilePic");
public final String PICTURE_DIRECTORY  = url.toString();

A URL contains a protocol, "file" in your case. The URL class' toString() simply returns the full URL, including protocol. You now have variable PICTURE_DIRECTORY containing "file:/C:/...". This is not a valid path on any Windows OS.

If you really want to write to a resource (not recommended), you could do:

public final String PICTURE_DIRECTORY  = toFile(url).getAbsolutePath(); 

static private File toFile(final URL url) {
    try {
        return new File(url.toURI());
    }
    catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    }
}

But note that a resource, as returned by Class..getResource(..), isn't always a file. It could just as well be an entry in a JAR (which you can't write to, in the same way as writing a file).

A better approach would probably be to use a directory relative to your users home directory, or other configured directory.

Finally, as I mentioned in the comments, using ImageIO for copying (image) files is usually the wrong solution. Instead, use Files.copy(...), as in the Copying a File or Directory tutorial.

Sign up to request clarification or add additional context in comments.

1 Comment

in addition: this would work across the platforms, since java figures out how to translate it to the os-specific location

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.