file - How can I indicate the path of a package in a String in Java? -
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"));
the 2 lines of code above taking kind of resource given paths. change them reference package within same netbeans project, contains same resources.
for example,
fileinputstream();
... getting audio file.
the
bufferedimage bf = imageio.read(new file());
... getting .jpg image.
these 2 files in package called 'resources' in same netbeans project. how change specified paths go directly packages, instead of going through hard disk?
thanks.
the class.getresource() , class.getresourceasstream() methods relative class location. intended purpose.
from javadoc
finds resource given name. rules searching resources associated given class implemented defining class loader of class. method delegates object's class loader. if object loaded bootstrap class loader, method delegates classloader.getsystemresource(java.lang.string). before delegation, absolute resource name constructed given resource name using algorithm: if name begins '/' ('\u002f'), absolute name of resource portion of name following '/'. otherwise, absolute name of following form: modified_package_name/name modified_package_name package name of object '/' substituted '.' ('\u002e').
a sample program acquire url file in src/resources/test.properties if exists.
public class testgetresource { public testgetresource(){ object resource = this.getclass().getresource("/test.properties"); system.out.println(resource); } public static void main(string args[]){ new testgetresource(); } }
to debug try this
object resource = this.getclass().getresource("/");
this should return binary path project. have in there netbeans should copy resources in there when makes project if doesn't null.
your directory structure should be
src/main/java
src/main/resources
Comments
Post a Comment