【问题标题】:How can I indicate the path of a package in a String in Java?如何在 Java 中的 String 中指示包的路径?
【发布时间】:2013-05-06 23:07:16
【问题描述】:
new FileInputStream("C:\\Users\\Adam\\Documents\\NetBeansProjects\\TicTacToe_3.0 beta\\src\\resources\\System Shock 2 soundtrack Med Sci 1.mp3");

BufferedImage bf = ImageIO.read(new File("C:\\Users\\Adam\\Documents\\NetBeansProjects\\TicTacToe_3.0 beta\\src\\images\\black-squareMod.jpg"));

上面的两行代码从给定的路径中获取某种资源。我想更改它们,以便它们引用同一 Netbeans 项目中的包,该项目包含相同的资源。

例如,

文件输入流();

...正在获取音频文件。

BufferedImage bf = ImageIO.read(new File());

...正在获取 .jpg 图像。

这两个文件位于同一个 Netbeans 项目中名为“resources”的包中。如何更改指定的路径,以便它们直接进入这些包,而不是通过我的硬盘?

谢谢。

【问题讨论】:

  • Class.getResource() 和 Class.getResourceAsStream() 方法是相对于类位置的。它们就是为此目的而设计的。
  • 这就是我的想法。我实现了它,但有一个异常指出 getResource() 返回 null。我确保源路径正确并列在项目类路径中。
  • 您不需要在 Java 文件名中使用反斜杠。使用/
  • @EJP 是的,这是正确的。当我尝试运行我的代码时,使用双反斜杠实际上会导致异常。

标签: java file package


【解决方案1】:

Class.getResource() 和 Class.getResourceAsStream() 方法是相对于类位置的。它们就是为此目的而设计的。

来自 javadoc

Finds a resource with a given name. The rules for searching resources associated
with a given class are implemented by the defining class loader of the class. This 
method delegates to this object's class loader. If this object was loaded by the 
bootstrap class  loader, the method delegates to ClassLoader.getSystemResource(java.lang.String).

Before delegation, an absolute resource name is constructed from the given resource
name using this algorithm:

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is 
the portion of the name following the '/'.

Otherwise, the absolute name is of the following form:
modified_package_name/name Where the modified_package_name is the package name of 
this object with '/' substituted for '.' ('\u002e').

一个示例程序,如果存在,它将获取 src/resources/test.properties 中文件的 url。

public class TestGetResource {

    public TestGetResource(){
        Object resource = this.getClass().getResource("/test.properties");
        System.out.println(resource);
    }

    public static void main(String args[]){
        new TestGetResource();
    }
}

调试试试这个

Object resource = this.getClass().getResource("/");

这应该返回项目的二进制路径。看看那里的NetBeans应该 当它制作项目时复制那里的所有资源,如果没有,那么你会得到 空值。

你的目录结构应该是

src/main/java
src/main/resources

【讨论】:

  • 我尝试了您的调试代码,方法是创建一个 Object 对象并使用 getResource() 方法,传递一个“/”的 URL。 Netbeans 立即在 .../build/class 打印出我的项目路径。我似乎无法使用 BufferedImage 对象或 FileInput 流来完成这项工作......要么构造函数不同意,要么 getResource() 方法返回 null。
  • @Adam Ali NetBeans 应该将资源移动到该目录中。在构建。如果不是,那么 getResource 将找不到它。
  • 你是对的,文件已被移动 - 现在我检查了那个文件夹。那么这有什么区别呢?我似乎仍然无法将 getResource() 与 FileInputStream 或 BufferedImage() 一起使用。我如何在这些类中使用它?
【解决方案2】:

我找到了解决办法:

new FileInputStream("C:\\Users\\Adam\\Documents\\NetBeansProjects\\TicTacToe_3.0 beta\\src\\resources\\System Shock 2 soundtrack Med Sci 1.mp3");

已改为

ClassLoader.getSystemResourceAsStream("resources/System Shock 2 soundtrack Med Sci 1.mp3");

这会处理音频。

至于 BufferedImage 对象:

BufferedImage bf = ImageIO.read(new File("C:\\Users\\Adam\\Documents\\NetBeansProjects\\TicTacToe_3.0 beta\\src\\images\\black-squareMod.jpg"));

改为

BufferedImage bf = ImageIO.read(ClassLoader.getSystemResource("images/black-squareMod.jpg"));

完美运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-21
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    相关资源
    最近更新 更多