Abhijit Kumbhar
Abhijit Kumbhar

Reputation: 961

How to resolve Image not found java.io.FileNotFoundException?

I'm giving input image to File for some purpose I stored Image and source file in same package but why I'm getting this error please help me ... my package is xyz and source file name image.java and image is stored in xyz/original.png(i.e. Image path), below is code snip of image.java...

File file = new File("original.png");
try {
    FileInputStream imageInFile = new FileInputStream(file);
    byte imageData[] = new byte[(int)file.length()];
    imageInFile.read(imageData);
}

My directory structure

Upvotes: 2

Views: 3935

Answers (2)

Abhijit Kumbhar
Abhijit Kumbhar

Reputation: 961

After searching a lot on stack overflow sites I found the solution and that is I placed my image may be in wrong directory :( using

String file_name = "original.png";
        File file = new File(file_name);
        System.out.println(file.getCanonicalPath());

I printed canonical path and this shows me that my image must be inside that path, After copying image at that path my code executed... thank you for your cooperation in resolving my problem.

Upvotes: 0

Priyamal
Priyamal

Reputation: 2989

InputStream input = classname.class.getResourceAsStream("IMAGE FILE");

try using above approach to load the file . just pass the file name if its in the same package.

Upvotes: 1

Related Questions