-2

I am an idiot. Why can't I read any files?

import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class mainClass {

    public static void main(String[] args) {
        try {
            Image picture = ImageIO.read(new File("picture.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

And the file is in the src folder for sure.

But it throws the exception everytime even though the file is there, no misspellings.

edit: The first sentence was true, I didn't put the image in the root.

wokparty
  • 1
  • 1
  • 1
  • 2

4 Answers4

3

Been there as well, know that feeling. Anyway, try print out current working directory and that will tell you exactly where is the application really read from.

try
{
    Image picture = ImageIO.read(new File("picture.png"));
}
catch (IOException e)
{
    String workingDir = System.getProperty("user.dir");
    System.out.println("Current working directory : " + workingDir);
    e.printStackTrace();
}
Jasonw
  • 5,054
  • 7
  • 43
  • 48
  • Relevant reference: [java.io.file docs](http://docs.oracle.com/javase/7/docs/api/java/io/File.html) Also, using `absolute path` (like `/home/Stuff/Pictures/picture.png`) worth a shot. In other hand , it's OS dependent. Note: `File` object has an [`getParent()`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#getParent()) method, which, in this case, might do the same. – Kamiccolo Apr 29 '14 at 15:43
  • Okay, that's a very constructive comment and that's positive. Lately, no idea why SO deducted user reputation without giving good reason. Lost my faith toward SO to actively contribute in the future. – Jasonw Apr 30 '14 at 04:31
0

Try to specify the path of your image. For example:

Image picture = ImageIO.read(new File("C:/Desktop/picture.png"));

Or if the image is in your workspace, for example:

Image picture = ImageIO.read(new File("C:/workspace/Project/src/picture.png"));

You can see the image location (the path of your image) clicking with the right button of the mouse in the image, then Properties.

You can use one slah ( / ) or two backslashs ( \\ ) to separate the directories of the path.

Thiago Souza
  • 1,093
  • 3
  • 13
  • 31
0

There is a dichotomy in java.

  1. If your file is part of the deliverable project, a file which can be packed in the application jar file, then it is a resource.

    Image picture = ImageIO.read(SomeClassOfYours.class.getResource("/picture.png"));
    

The path is relative to that class, and can be made an absolute path. All on the java class path of the application.

  1. If your file is a file system file, a File, then the path best is a full absolute path. A relative path depends at which working directory the application is run, and that might differ.

    Image picture = ImageIO.read(new File("C:/.../src/picture.png"));
    

The question seems to indicate that the image is a read-only part of the application, in which case it is a resource file. The path must be case sensitive, and the path separator must be a forward slash /.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

The ImageIO.read() function will check whether an image is corrupted or not. If it's corrupted, it will throw an exception.

ImageIO.read(new File("C:/Desktop/picture.jpeg"));
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Chinna
  • 1