在资源文件夹中以字符串形式获取文件的位置 [英] Getting location of file as string in resource folder

查看:171
本文介绍了在资源文件夹中以字符串形式获取文件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用eclipse完成了一个程序,现在我想将其导出为单个可运行的jar文件.该程序包含一个资源文件夹,其中包含图像和文本文件.它位于源文件夹下.

I have completed a program in eclipse and now I would like to export it as a single runnable jar file. The program contains a resource folder with images and text files in it. This is located beneath the source folder.

res文件没有添加到构建路径,但是当我在Eclipse中运行程序时,它仍然可以工作. 令我感到困惑的是,导出文件时res文件正在保存到可运行的jar文件中,因为我可以使用WinRar打开Jar文件,并且看到文件夹中包含所有对象.但是,当我运行问题时,它会在引用资源文件夹的那一刻停止.手动复制并粘贴可运行jar文件的位置旁边的res文件夹并运行程序时,要引起我的困惑,

The res file is not added to the build path however when I run the program in Eclipse it still works. The thing that is confusing me is that the res file is being saved into the runnable jar file when I export it as I can open the Jar file with WinRar and I see the folder is there with all the objects in it. But when I run the problem it stops at the point that the resource folder is referenced. To add to my confusion when I manually copy and paste the res folder next to where the runnable jar file is saved and run the program it works exactly as it should do.

现在,我知道这与我在代码中引用文件的方式有关.目前我有这样的

Now I know this is something to do with how I reference the files in my code. At the moment I have it like this

reader = new LineNumberReader(new FileReader("res/usernames.txt"));

这完全符合我的需要,并且在Eclipse中以及当我将资源文件夹移到Jar文件旁边时,都毫无例外地访问res文件夹.

This works exactly how I want and accesses the res folder without any exceptions - in Eclipse and when I move the resource folder next to the Jar file.

我希望它能正常工作,但是在Jar文件之外没有文件夹,我希望它们都封装在一个Jar文件中.

I would like it to work normally but without having a folder outside of the Jar file I would like it all encapsulated in one Jar file.

我做了很多研究,这似乎是一个常见的解决方法-我想补充一点,我真的不知道它是如何工作的,但每个人似乎都提到它-用于某个地方:

I did a lot of research and what seems to be a common fix - may I add I don't really know how it works but everyone seems to mention it - is to somewhere use:

myClass().getResource()

当我创建一个新的FileReader时,它需要一个String输入,但是当我使用myClass().getResource()时,它将返回一个资源而不是一个字符串.我也不清楚如何引用资源文件夹.我应该将资源文件夹移到源文件夹吗?

When I create a new FileReader it needs a String input however when I use myClass().getResource() it returns a resource and not a string. I also don't have a clue how it is meant to reference the resource folder. Should I move the resource folder into the source folder?

有人知道我如何从可运行的jar文件中引用资源文件夹吗?

Does anyone know how I can reference the resource folder from within the runnable jar file?

很抱歉提出一个问题,我知道我想要最终产品什么,但是我对构建路径和从类内部进行引用感到困惑,并且我已经在网上进行了很长时间的搜索,试图弄清楚它.

Sorry for rambling question I know what I want for my final product but I'm getting confused by the build paths and referencing from within classes and I have searched online for a long time trying to figure it out.

推荐答案

部署软件时,资源不会作为文件部署在文件夹中.它们被打包为应用程序jar的一部分.您可以通过从罐子内部检索它们来访问它们.类加载器知道如何从jar中检索内容,因此您可以使用类的类加载器来获取信息.

Resources, when you deploy your software, are not deployed as files in a folder. They are packaged as part of the jar of your application. And you access them by retrieving them from inside the jar. The class loader knows how to retrieve stuff from the jar, and therefore you use your class's class loader to get the information.

这意味着您不能在其上使用诸如FileReader之类的东西,也不能使用任何其他需要文件的东西.资源不再是文件.它们是装在jar中的字节束,只有类加载器才知道如何检索.

This means you cannot use things like FileReader on them, or anything else that expects a file. The resources are not files anymore. They are bundles of bytes sitting inside the jar which only the class loader knows how to retrieve.

如果资源是诸如图像等之类的东西,可以由知道如何访问资源URL的Java类使用(即,在给定其在jar中的位置时从jar中获取数据),则可以使用ClassLoader.getResource(String)方法获取URL并将其传递给处理它们的类.

If the resources are things like images etc., that can be used by java classes that know how to access resource URLs (that is, get the data from the jar when they are given its location in the jar), you can use the ClassLoader.getResource(String) method to get the URL and pass it to the class that handles them.

如果您想直接对资源中的数据进行任何操作(通常是通过从文件中读取数据来完成),则可以改用方法ClassLoader.getResourceAsStream(String).

If you have anything you want to do directly with the data in the resource, which you would usually do by reading it from a file, you can instead use the method ClassLoader.getResourceAsStream(String).

此方法返回一个InputStream,您可以像使用其他任何InputStream一样使用它-将Reader应用于它或类似的东西.

This method returns an InputStream, which you can use like any other InputStream - apply a Reader to it or something like that.

因此您可以将代码更改为:

So you can change your code to something like:

InputStream is = myClass().getResourceAsStream("res/usernames.txt");
reader = new LineNumberReader( new InputStreamReader(is) );

请注意,我使用的是Class中的getResourceAsStream()方法,而不是ClassLoader.这两个版本在jar中查找资源的方式略有不同.请阅读 Class

Note that I used the getResourceAsStream() method from Class rather than ClassLoader. There is a little difference in the way the two versions look for the resource inside the jar. Please read the relevant documentation for Class and ClassLoader.

这篇关于在资源文件夹中以字符串形式获取文件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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