如何在Java中以字符串表示包的路径?

如何在Java中以字符串表示包的路径?,java,file,package,Java,File,Package,上面的两行代码从给定的路径获取某种资源。我想更改它们,以便它们引用同一个Netbeans项目中的包,该项目包含相同的资源 例如 FileInputStream() 。。。正在获取音频文件 BuffereImage bf=ImageIO.read(新文件()) 。。。正在获取.jpg图像 这两个文件位于同一个Netbeans项目中名为“resources”的包中。我如何更改指定的路径,使它们直接进入这些包,而不是通过我的硬盘 谢谢。Class.getResource()和Class.getRes

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

例如

FileInputStream()

。。。正在获取音频文件

BuffereImage bf=ImageIO.read(新文件())

。。。正在获取.jpg图像

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

谢谢。

Class.getResource()和Class.getResourceAsStream()方法是相对于类位置的。它们就是为了这个目的

来自javadoc

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"));
示例程序将获取src/resources/test.properties中文件的url(如果存在)

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').
若要调试,请尝试以下操作

public class TestGetResource {

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

    public static void main(String args[]){
        new TestGetResource();
    }
}
这将返回项目的二进制路径。看一看,NetBeans应该 当它创建项目时,复制其中的所有资源,如果没有,那么您将得到 空值

您的目录结构应该是

src/main/java

src/main/resources

Class.getResource()和Class.getResourceAsStream()方法与类位置相关。它们就是为了这个目的

来自javadoc

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"));
示例程序将获取src/resources/test.properties中文件的url(如果存在)

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').
若要调试,请尝试以下操作

public class TestGetResource {

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

    public static void main(String args[]){
        new TestGetResource();
    }
}
这将返回项目的二进制路径。看一看,NetBeans应该 当它创建项目时,复制其中的所有资源,如果没有,那么您将得到 空值

您的目录结构应该是

src/main/java


src/main/resources

我找到了解决方案:

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

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"));
这会处理音频

对于BuffereImage对象:

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

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"));

非常有效。

我找到了解决方案:

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

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"));
这会处理音频

对于BuffereImage对象:

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

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"));

工作正常。

Class.getResource()和Class.getResourceAsStream()方法与类位置相关。他们就是为了这个目的,这就是我的想法。我实现了它,但得到一个异常,声明getResource()返回null。我确保源路径正确并列在项目类路径中。您不需要在Java文件名中使用反斜杠。使用
/
@EJP Yes,这是正确的。当我试图运行代码时,使用双反斜杠实际上会导致异常。Class.getResource()和Class.getResourceAsStream()方法是相对于类位置的。他们就是为了这个目的,这就是我的想法。我实现了它,但得到一个异常,声明getResource()返回null。我确保源路径正确并列在项目类路径中。您不需要在Java文件名中使用反斜杠。使用
/
@EJP Yes,这是正确的。当我试图运行代码时,使用双反斜杠实际上导致了异常。我尝试了调试代码,创建了一个Object对象,并使用getResource()方法,传递了一个URL“/”。Netbeans立即在…/build/class打印出我的项目路径。我就是不能用BuffereImage对象或FileInput流来实现这一点。。。要么构造函数不同意,要么getResource()方法返回null。@Adam Ali NetBeans应该将资源移动到该目录中。建造中。如果不是,那么getResource将找不到它。你是对的,文件已经被移动了-现在我检查了该文件夹。那么这又有什么区别呢?我似乎仍然无法将getResource()与FileInputStream或BuffereImage()一起使用。如何在这些类中使用它?我尝试了您的调试代码,创建了一个对象并使用getResource()方法,传递了一个URL“/”。Netbeans立即在…/build/class打印出我的项目路径。我就是不能用BuffereImage对象或FileInput流来实现这一点。。。要么构造函数不同意,要么getResource()方法返回null。@Adam Ali NetBeans应该将资源移动到该目录中。建造中。如果不是,那么getResource将找不到它。你是对的,文件已经被移动了-现在我检查了该文件夹。那么这又有什么区别呢?我似乎仍然无法将getResource()与FileInputStream或BuffereImage()一起使用。我如何在这些类中使用它?