Get a file inside a specific package

10,075

Solution 1

Use this:

ClassA.class.getResourceAsStream("/packageB/yourfile.ext");

Solution 2

Use the import statement.

For example, to import class Bar from package a.b.c.d into your class in package a.b.c, you would write:

package a.b.c;

import a.b.c.d.Bar;

class Foo {
}
Share:
10,075
Lorenzo B
Author by

Lorenzo B

Software Engineer | PhD in Computer Science

Updated on August 02, 2022

Comments

  • Lorenzo B
    Lorenzo B almost 2 years

    I've a java package (package A) and I would retrieve a file contained into another package (package B). For example, suppose that into package A I have a class named ClassA. The ClassA needs to access an xml file (i.e. a configuration file) contained into package B.

    What is the procedure that I can use to access the configuration file from ClassA?

    Thank you in advance. FA.