java - Reading a file from resource package -


i have read many posts on here , done many google searches still unable read file java package within current project.

in 1 of classes have following code, input stream null. have tried using context class loader, many other solutions no avail.

inputstream = this.getclass().getresourceasstream( "opcodes.txt" );   system.out.println(is); 

another attempt was:

inputstream = classname.class.getresourcceasstream("/resources/opcodes.txt"); 

which returned null.

any or explanation why can not find file within resource package great.

p.s. using eclipse if makes difference.

edit: if use openfiledialog find file, able open , read it, file exist , not corrupt.

the documentation of getresourceasstream() method, found here: http://docs.oracle.com/javase/7/docs/api/java/lang/class.html#getresourceasstream(java.lang.string) says that: " 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'). "

your first attempt have succeeded had resource been located within same package 1 class you're invoking method located. second attempt fails because, documentation says, name of file give ("/resources/opcodes.txt") replaced "resources/opcodes.txt". guess means method search package resources within package class invokes method located , not outside of it. since don't have such inner package, method returns null.

a workaround define class within package resource located. class empty care. call:

classwithinresourcepackage.class.getresourceasstream("opcodes.txt"); 

from within class invoke method from, , works.

i tried use ".." syntax know command line, doesn't work. think documentation implies method looking file within same package only.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -