poudelpavan
poudelpavan

Reputation: 89

Can't read input file, IOException

I'm trying to make the array of image buttons in a panel in Jform. For this i want to add the image by giving the path but here is the IO Exception that can't read input file. The error occurs as

Exception in thread "main" javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at serverui.ImageArray.main(ImageArray.java:38)

The code is as follow: package serverui;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ImageArray {
    Image[] images;

    public ImageArray(Image[] images) {
        this.images = images;
    }

    private JPanel getContent() {
        JPanel panel = new JPanel(new GridLayout(0,2,5,5));
        panel.setBackground(Color.green.darker());
        for(int i = 0; i < images.length; i++) {
            ImageIcon icon = new ImageIcon(images[i]);
            JLabel label = new JLabel(icon, JLabel.CENTER);
            panel.add(label);
        }
        return panel;
    }

    public static void main(String[] args) throws IOException {

        String prefix = "images/";
        String[] ids = { "desktopactive", "desktopoff" };
        String ext = ".png";
        Image[] images = new Image[ids.length];
        for(int i = 0; i < images.length; i++) {
            String path = prefix + ids[i] + ext;
            images[i] = ImageIO.read(new File(path));
        }

        ImageArray app = new ImageArray(images);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(app.getContent());
        f.pack();
        f.setLocation(200,200);
        f.setVisible(true);
    }
}

Upvotes: 2

Views: 8967

Answers (3)

Ion
Ion

Reputation: 334

Well it depends on your platform, if this is indeed on windows. then you need to change it to \ on

String prefix = "images/"

What you can do though if you want is create an if statement that modifies the string based on the operating system. The way you get what operating system you have is through

System.getProperty("os.name")

Upvotes: 0

ThomasEdwin
ThomasEdwin

Reputation: 2145

You should make sure that both files exist.

    for(int i = 0; i < images.length; i++) {
        String path = prefix + ids[i] + ext;
        File file = new File(path);
        if (!file.exists()) {
            throw new IllegalArgumentException("file "+file+" does not exist");
        }
        images[i] = ImageIO.read(file);
    }

Make sure that images is relative to your current directory. If you put images in your classpath, you may use this:

    for(int i = 0; i < images.length; i++) {
        String path = prefix + ids[i] + ext;
        URL url = ImageArray.class.getResource(path);
        if (url == null) {
            throw new IllegalArgumentException("url "+url+" does not exist");
        }
        images[i] = ImageIO.read(url);
    }

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

Change your code as follows, Issue in path

   String prefix = "D:\\TestFolder\\";
    String[] ids = { "Capture"};
    String ext = ".png";
    Image[] images = new Image[ids.length];
    for(int i = 0; i < images.length; i++) {
        String path = prefix + ids[i] + ext;
        images[i] = ImageIO.read(new File(path));
    }

In this way it is working for me. Some times if you provide a prefix as folder in C drive(operating system install partition), you may unable to read that file due to privileges issue. Else in above will solve your matter.

Upvotes: 1

Related Questions