如何在项目中打包资源文件 [英] How to package resource files within project

查看:158
本文介绍了如何在项目中打包资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否正确执行了此操作. 我想在我的Java项目中有一个包含一堆文本文件的文件夹或软件包.在代码中,我希望列出文件夹中的所有文件,并能够读取它们.我希望这些资源文件成为我项目的目录结构的一部分(以便可以使用我的源代码对它们进行版本控制).

I'm not sure if I'm doing this correctly. I want to have a folder or package in my Java project that contains a bunch of text files. In code, I want to list all of the files in the folder, and be able to read them. I want these resource files to be part of my project's directory structure (so that they can be version controlled with my source).

我看过ClassLoader.* resource方法,但是无法在源目录中标识文件夹并列出其内容...

I have seen the ClassLoader.*resource methods but can't identify a folder in my source directories and list it's contents...

顺便说一句,我正在使用Eclipse. 有人知道如何实现这个目标吗?

BTW I'm using Eclipse. Does anybody know how to do achieve this goal?

谢谢

========================================

==========================================

更新:

感谢您的答复!我发现通过添加源文件夹,我只能按名称访问其中的文件.但是,这不符合我的需求,因为文件夹的内容将更改,并且我希望能够动态读取所有内容,而不必每次更改内容时都更改读取逻辑.我尝试为源文件夹中的常规文件夹创建一个文件,并遍历其内容.只要可以在Eclipse中运行,它就可以运行,但是根据Edwin的评论,当我导出到JAR时,这将不起作用,所以我回到了第1方.

Thanks for the replies! I found that by adding the source folder, I can only access the files inside by name. This doesn't fit my needs however, as the contents of the folder will change, and I want to be able to read all the contents dynamically, without having to change the read logic every time the contents change. I tried creating a File for a regular folder within the source folder and iterating over it's contents. That works as long as run in Eclipse, but as per Edwin's comment, this will not work when I export to a JAR, so I'm back at square one.

我在想,必须有一种方法可以在我的项目中包括可以通过编程方式访问的资源文件(Android是否这样做,并且它基于Java,对吗?).那么,还有另一种方法来创建一个资源"包,其中包含一堆我可以在运行时访问而无需更新代码以查找每个文件的文件吗?我真的不想将此文件夹与我的源分开并将其保存在其他位置,因为这会使部署复杂化... 有人有什么主意吗?

I'm thinking there MUST be a way to include resource files in my project that I can access programmatically (Android does this and it's based on java right?). So is there another way to create a 'resources' package containing a bunch of files that I can access at runtime without having to update code to look for each file? I really don't want to have to separate this folder from my source and maintain it in a different location as this will complicate deployment... Does anybody have any ideas?

谢谢

==================================

====================================

解决方案:

我刚刚意识到,Android在编译时会盘点资源文件,以使其在运行时可用.所以这可能是要走的路:我将在我的Ant脚本中添加一个步骤,以写入一个包含文件夹中文件列表的文件(总是命名为相同文件,以便我可以访问它).这样,我将知道所有可用资源文件的名称.感谢Edwin的帮助:)

I just realized that Android inventories it's resource files at compile time in order to make them available at runtime. So this is probably the way to go: I'll just add a step to my Ant script that writes a file (always named the same so i can access it) containing a list of the files in my folder. This way I'll know the names of all my available resource files. Thanks Edwin for your help :)

推荐答案

在Eclipse Build Path对话框中,确保您的资源文件夹是源文件夹.然后,您将能够使用问题中提到的ClassLoader方法来加载它们.

Make sure your resource folder is a source folder in the Eclipse Build Path dialog box. Then you will able to load them with the ClassLoader method that you mentioned in the question.

如果使用此技术,则源文件夹中的每个子文件夹都被视为一个包.因此,请确保在加载资源时将其考虑在内.

If you use this technique, every subfolder within the source folder is considered a package. So make sure to take that into account when you load the resource.

此技术基本上确保该文件夹中的所有Java文件都已编译并放置在分发文件夹下.并将所有其他文件简单地从此文件夹复制到分发文件夹(即bin或classes文件夹).

This technique basically makes sure that all Java the files in this folder are compiled and placed under the distribution folder. And any other files are simply copied from this folder to the distribution folder (i.e. the bin or classes folder).

稍后,当您将项目打包到jar或war中时,必须确保此文件包含在打包文件中.

Later, when you package your project into a jar or war, you have to make sure this files are included within the packaged file.

这篇关于如何在项目中打包资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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