无法使用文件:前缀构造具有相对文件路径的图像对象 [英] Cannot Construct An Image Object With Relative File Path WIthout Using file: Prefix

查看:116
本文介绍了无法使用文件:前缀构造具有相对文件路径的图像对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建这样的图像对象:

I have been trying to create an image object like this:

Image img = new Image("images/jack.png");

Image img = new Image("jack.png");

/jack.png /images/jack.png 等。

我使用 System查找了工作目录。 getProperty(user.dir),它确实是我放置图像文件的地方。当我使用 file:前缀时,它确实有效,如下所示:

I have looked up the working directory using System.getProperty("user.dir") and it is indeed where I put my image file. When I use file: prefix, it does work, like so:

Image img = new Image("file:images/jack.png");

然而,它也应该在不使用它的情况下工作。在教科书中,它没有文件:。我已经看到其他代码在没有它的情况下工作。

However, it is also supposed to work without using it. In the textbook it is done without file:. I've seen other codes that work without it.

在一堆链式异常结束时,它说:

At the end of a bunch of chained exceptions, it says:

引起:java.lang.IllegalArgumentException:找不到无效的网址或资源

我也尝试过从 OpenJDK 中读取源代码,我可以解决任何问题,因为很多方法都是原生的,从我追踪到的,我不明白它是如何工作的。另外,我可以用同样的方式创建文件,我只是无法创建图像。例如,这有效:

I also tried to read source code from OpenJDK and I could figure anything out because many methods were native and from what I traced I didn't understand how it didn't work. Also, I can create files the same way, I just can't create images. For instance, this works:

File file = new File("fileName.txt");

导致此问题的原因是什么,我应该怎么做才能修复它?

What causes this problem, what should I do to fix it?

我正在使用NetBeans,如果这很重要。

I'm using NetBeans, if that matters.

推荐答案

请注意 System.getProperty(user.dir)不返回工作目录。它返回用户目录。

可以使用 File 构造函数中的相对文件路径指定相对于工作目录的路径。然而,依赖工作目录是不好的做法。从NetBeans启动应用程序会导致工作目录成为项目目录,但情况并非如此,如果以不同的方式启动。

Note that System.getProperty("user.dir") does not return the working directory. It returns the user directory.
A path relative to the working directory can be specified using a relative file path in the File constructor. However it's bad practice to rely on the working directory. Starting the application from NetBeans results in the working directory being the project directory, but this is not the case, If started in a different way.

应用程序中需要的图像因此应该添加到jar中。

在这种情况下,您可以通过 URL .com / javase / 8 / docs / api / java / lang / Class.html#getResource-java.lang.String-rel =nofollow> Class.getResource() 。 (转换为字符串使用 toExternalForm()。)

Images you need in your application should therefore be added to the jar.
In this case you can retrieve the image URL via Class.getResource(). (convert to String using toExternalForm().)

如果您有引用图像文件的文件,则可以使用文件实例获取网址

If you have a File that references a image file, you can use the File instance to get a URL:

File file = ...
String urlString = file.toURI().toURL().toExternalForm();

这些网址可以与图片一起使用构造函数。

Those URLs can be used with the Image constructor.

请注意

File file = new File("fileName.txt");

创建文件。它只代表一个文件路径。此文件可能存在,也可能不存在。只需调用文件构造函数就不会创建新构造函数。

does not create a file. It just represents a file path. This file may or may not exist. Simply invoking the File constructor does not create a new one.

这篇关于无法使用文件:前缀构造具有相对文件路径的图像对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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