在Java中使用资源文件 [英] Using resource files in Java

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

问题描述

我正在使用Java资源文件,我有几个问题...


  1. 我希望将我的程序分发给别人,我假设JAR文件不是最好的方法。我可能会通过转换为exe来实现这个优点吗?有什么限制?

  2. 如果我转换为exe,它会保留资源文件?

  3. 我其实只是试图使用一个资源文件。该文件是一个文本文件,只会保存某些文件的用户目录,以便每次打开程序时都不需要设置它们。这是甚至是最好的方式吗?

  4. 你如何在代码本身中引用资源文件?

这是我做了什么
创建了一个新的资源文件,因为我使用Netbeans,我可以在导航器的文件选项卡下看到它的位置,如下所示:




  • 主项目


    1. 构建

    2. 课程

    3. myclass

    4. 资源

      • directories.txt


    li>



这是我正在尝试访问它,但是当我调试它将返回null。

  private void getPaths()// todo 
{

try
{
InputStream is = getClass ().getResourceAsStream( /资源/ directories.txt);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
字符串;
while((line = br.readLine())!= null)
{
System.out.println(line);
}
br.close();
isr.close();
is.close();
}
catch(例外e)
{

}
}


解决方案

转换为EXE只是一种幻想的方式来说将jar文件包装到可执行容器



我假设JAR文件不是最好的方式不是真的。有时候提供一个操作系统特定的启动程序是很好的,但并不总是最好的解决方案。



有什么限制? EM>。那么首先,你将自己限制在一个平台上。对于Mac,您需要将应用程序捆绑到应用程序包中。对于linux,我想大多数人提供脚本来启动他们的代码。



你也可以将自己限制在特定的位深度。如果您提供的是一个x32位可执行文件,那么您只能在x32位环境中运行。这可能不是一个问题,但是您限制可用的内存开始...



所以是的,一般来说,您的资源文件将是安全的。 p>

资源文件通常嵌入。您在第3部分中描述的更类似于配置文件。该文件需要存储在文件系统(exe / jar的外侧),以便可以轻松更新。



你如何引用资源文件在代码本身?



对于嵌入式资源,您将需要使用 getClass()getResource(。 ..)。对于你的配置文件,我会像任何其他文件一样说...



我也可以看一下部署关于部署Java程序的建议机制的一些想法,


I'm just getting into using "Java Resource Files" and i have a few questions...

  1. I am hoping to distribute my program to others and I'm assuming JAR file isn't the best way. I'd probably go about it by "converting to exe" is this good practice? what are the limitations?
  2. If I convert to an exe does it keep the resource files?
  3. I'm actually just trying to use a resource file. This file is a text file and will just save the users directories for certain files so they don't need set them up every time they open the program. is this even the best way to go about it?
  4. how do you reference the resource file in the code itself?

Here is what I've done. created a new resource file and since I'm using Netbeans I can see its location under the files tab in the navigator it looks like this:

  • Mainproject

    1. build
    2. classes
    3. myclass
    4. resources
      • directories.txt

here is how i'm trying to access it but when i debug it is coming back null.

private void getPaths()//todo
{   

try
{
 InputStream is = getClass().getResourceAsStream("/resources/directories.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) 
{
    System.out.println(line);
}
br.close();
isr.close();
is.close();
}
 catch(Exception e)
  {

    }
}

解决方案

"Converting to EXE" is just a fancy way of saying "wrapping the Jar files into an executable container"

"I'm assuming JAR file isn't the best way" not really. It's nice to provide a OS specific means for launching the program at times, but it's not always the best solution.

"what are the limitations?". Well, to start with, you're limiting your self to a single platform. For Mac's you need to bundle the application into a "app" bundle. For linux, I think most people provide scripts to launch their code.

You could also be limiting your self to particular bit depth. If all you supply is a x32 bit executable, then you'll only ever run within a x32 bit environment. This may not be an issue, but you're limiting the available memory to start with...

So yes, generally, your resource files will be safe.

A resource file is generally embedded. What you're describing in part 3 is more akin to a configuration file. This file needs to be stored on the file system (out side of your exe/jar) so it can easily be updated.

"how do you reference the resource file in the code itself?"

For embedded resources you will need to start by using getClass().getResource(...). For you configuration file, I'd say just like any other file...

I would also have a look at Deployment some ideas on the suggest mechanisms for deploying Java programs,

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

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