了解"ImageIO.read(getClass().getResource(path))"; [英] Understanding "ImageIO.read(getClass().getResource(path))"

查看:284
本文介绍了了解"ImageIO.read(getClass().getResource(path))";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于以下代码示例的:

My question is about the following code-example:

    public class BufferedImageLoader {

        private BufferedImage image;

        public BufferedImage loadImage(String path) throws IOException {

            image = ImageIO.read(getClass().getResource(path));
            return image;

        }

    }

我查看了Java-API,并在ImageIO类中找到了3种不同的read()方法:

I looked in the Java-API and found 3 different read() methods in the ImageIO Class:

1 .: 读取(文件输入)

1.: read(File input)

2 .:读取(ImageInputStream流)

2.: read(ImageInputStream stream)

3 .:读取(InputStream输入)

3.: read(InputStream input)

4 .:读取(URL输入)

4.: read(URL input)

我的问题是:在此示例中使用了哪种方法中的四种? 我有点困惑,因为在示例中立场

My question is: Which of them four methods is used in this example? I'm a little bit confused, because in the example stands

read(getClass().getResource(path));

"getClass()"在这里返回"BufferedImageLoader",对吗? 然后我们将方法称为"read(getClass().getResource(path))",该方法必须位于BufferedImageLoader类中,但事实并非如此!

"getClass()" returns here "BufferedImageLoader", right? Then we call the method "read(getClass().getResource(path))", which must stand in the BufferedImageLoader Class, but this is not the case!

我错了吗?

推荐答案

getClass().getResource(path))返回URL,因此在这种情况下,它将使用ImageIO.read(URL)

getClass().getResource(path)) returns a URL, so in this case, it would using ImageIO.read(URL)

此外,如果您使用Class#getResourceAsInputStream,它将返回一个InputStream,这意味着它将使用ImageIO.read(InputStream)代替

In addition, if you used Class#getResourceAsInputStream, it would return an InputStream, meaning it would be using ImageIO.read(InputStream) instead

这篇关于了解"ImageIO.read(getClass().getResource(path))";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆