Java 打开图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9712160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening an image
提问by wokparty
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.
并且该文件肯定在 src 文件夹中。
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.
编辑:第一句话是对的,我没有把图像放在根目录中。
回答by Jasonw
Been there as well, know that feeling. Anyway, try print out current working directory and that will tell you exactlywhere 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();
}
回答by Thiago Souza
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.
您可以使用一个斜杠 ( / ) 或两个反斜杠 ( \\ ) 来分隔路径的目录。
回答by Joop Eggen
There is a dichotomy in java.
java中有一个二分法。
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"));
如果你的文件是可交付项目的一部分,一个可以打包在应用程序jar文件中的文件,那么它就是一个资源。
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.
该路径是相对于该类的,可以设为绝对路径。全部在应用程序的 java 类路径上。
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"));
如果您的文件是文件系统文件a
File
,那么路径最好是完整的绝对路径。相对路径取决于运行应用程序的工作目录,这可能会有所不同。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 mustbe case sensitive, and the path separator mustbe a forward slash /
.
这个问题似乎表明图像是应用程序的只读部分,在这种情况下它是一个资源文件。路径必须区分大小写,路径分隔符必须是正斜杠/
。