Java资源文件 [英] Java resource files

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

问题描述

我正在编写一个包含一些编辑器功能的小型GUI应用程序,我想让用户打开一些示例文本文件以快速测试。最简单的方法是使用适当的示例文件打包一个单独的zip,并让它们手动打开;我想让事情变得更加用户友好,并允许他们从应用程序内部选择文件,然后运行它们。

I'm writing a small GUI app that contains some "editor" functionality, and something that I'd like to let users open a few sample text files to test things out quickly. The easiest way of doing this would be to package a separate zip with the appropriate sample files, and have them open them manually; I'd like to make things a little more user-friendly and allow them to pick the files from inside the application and then run them.

那么...做什么我用?我最初考虑过.properties,但这似乎不太适合这项工作......

So... what do I use? I initially considered .properties but that doesn't seem terribly well suited for the job...

推荐答案

你可以包括一个资源将文件放在jar中,然后在应用程序中将其作为资源流打开。如果您正在使用Spring,则可以将资源直接注入bean中。如果没有,请查看 Class.getResourceAsStream()。您只需要注意用于获取资源文件的路径。

You can include a resource file right in the jar and then open it as a resource stream in your app. If you're using Spring, you can inject the resource right into a bean. If not, check out Class.getResourceAsStream(). You just have to be careful about the path you use to get to the resource file.

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

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