通过 getClass().getResource() 加载文件 [英] File loading by getClass().getResource()

查看:33
本文介绍了通过 getClass().getResource() 加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了使用getClass.getResource(path)加载资源文件的方式.代码片段在这里:

I have followed the way of loading the resource file by using getClass.getResource(path). The snippet of code is here :

String url = "Test.properties";

System.out.println("Before printing paths..");
System.out.println("Path2: "+ getClass().getResource(url).getPath());

FileInputStream inputStream = new FileInputStream(new File(getClass().getResource(url).toURI()));
i_propConfig.load(inputStream);
inputStream.close();

我已经在 eclipse 中配置了它的层次结构(在源下有一个名为 SwingDemo 的文件夹.在 SwingDemo 中有我的 java 文件以及资源文件)...

I have configured it in eclipse with the hierarchy (Under source there is a folder called SwingDemo. In SwingDemo there is my java file as well as the resource file)...

  1. 源代码
    • SwingDemo
  1. src
    • SwingDemo
  1. CustomDialog.java
  2. 测试属性

当我在 eclipse 上运行它时,一切都运行良好.但是一旦我尝试从 cmd 行运行应用程序,就会发生空指针异常..

When I am running this on eclipse everything is running fine. But as soon as I attempt to run the apps from cmd line null pointer exception is occuring..

命令行部署层次如下:

文件夹:D:WorkJava ProgrmsSwingDemo

层次结构:

  1. SwingDemo
    • CustomDialog.java
    • 测试属性

首先,我从命令行 (javac CustomDialog.java) 在 SwingDemo 文件夹中编译了这个文件.然后我移一步回到 Java Programs 文件夹(正如我提到的 .java 类中的包)并使用著名的

First of all I compiled this file inside SwingDemo folder from command line (javac CustomDialog.java). Then I move one step back to Java Programs folder (as I mentioned the package inside .java class) and run the apps by using the famous

java SwingDemo.CustomDialog

我以前在使用 new FileInputStream("path") 时也遵循类似的步骤.这样做之后,我得到了空指针异常..

I used to follow similar steps when I used new FileInputStream("path") previously. After doing this fashion I am getting null pointer exception..

我认为 getClass().getResource(url) 无法从特定目录加载文件.这就是为什么我将资源放在与我的 java 文件相同的目录中.它在 Eclipse 中运行良好.但是为什么当我从命令行运行时会出错.

I think getClass().getResource(url) cannot load file from a specific directory. That's why I put the resource in same directory as that of my java file. It ran fine in Eclipse. But why this is giving error when I run from Command Line.

推荐答案

getClass().getResource() 使用类加载器来加载资源.这意味着资源必须在要加载的类路径中.

getClass().getResource() uses the class loader to load the resource. This means that the resource must be in the classpath to be loaded.

使用 Eclipse 时,您放入源文件夹中的所有内容都由 Eclipse编译":

When doing it with Eclipse, everything you put in the source folder is "compiled" by Eclipse:

  • .java 文件被编译成 .class 文件进入 bin 目录(默认)
  • 其他文件被复制到 bin 目录(尊重包/文件夹结构)

当使用 Eclipse 启动程序时,bin 目录因此在类路径中,并且由于它包含 Test.properties 文件,因此类加载器可以使用 getResource() 加载该文件或 getResourceAsStream().

When launching the program with Eclipse, the bin directory is thus in the classpath, and since it contains the Test.properties file, this file can be loaded by the class loader, using getResource() or getResourceAsStream().

如果它在命令行中不起作用,那是因为该文件不在类路径中.

If it doesn't work from the command line, it's thus because the file is not in the classpath.

注意你不应该这样做

FileInputStream inputStream = new FileInputStream(new File(getClass().getResource(url).toURI()));

加载资源.因为只有从文件系统加载文件时才能工作.如果您将您的应用程序打包到一个 jar 文件中,或者您通过网络加载类,它将无法工作.要获得 InputStream,只需使用

to load a resource. Because that can work only if the file is loaded from the file system. If you package your app into a jar file, or if you load the classes over a network, it won't work. To get an InputStream, just use

getClass().getResourceAsStream("Test.properties")

最后,如文档所示,

Foo.class.getResourceAsStream("Test.properties")

将加载与 Foo 类位于同一包中的 Test.properties 文件.

will load a Test.properties file located in the same package as the class Foo.

Foo.class.getResourceAsStream("/com/foo/bar/Test.properties")

将加载位于 com.foo.bar 包中的 Test.properties 文件.

will load a Test.properties file located in the package com.foo.bar.

这篇关于通过 getClass().getResource() 加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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