getResourceAsStream - Zugriff auf Ressourcen in oberstem Package

M

Magnus Warker

Hallo,

wie kann man denn auf eine Ressource (File) zugreifen, das im obersten
Package liegt bzw. in einem Package ohne Klassen?

Ich habe versucht, von einer existierenden Klasse nach oben zu wandern
("../"), aber das klappt nicht:

Class c = getClass();
ClassLoader l = c.getClassLoader ();
InputStream s = c.getResourceAsStream ("../test.cfg");

Magnus
 
J

John B. Matthews

Magnus Warker said:
wie kann man denn auf eine Ressource (File) zugreifen, das im
obersten Package liegt bzw. in einem Package ohne Klassen?

Ich habe versucht, von einer existierenden Klasse nach oben zu
wandern ("../"), aber das klappt nicht: [Google translation]
how can you access a resource (file), the highest in the
Package is in a package or without classes?

I have tried from an existing class to move upwards
("../"), But that does not work: [Google translation]
Class c = getClass();
ClassLoader l = c.getClassLoader ();
InputStream s = c.getResourceAsStream ("../test.cfg");

Try c.getResourceAsStream() or c.getResource() [1]; these methods use
"/" to signify the top level. For example, GoogleOlympiad [2] has a
single class in the default package, so the "/" is optional. In
contrast, RobotChase [3] uses a conventional package hierarchy, so the
"/" is required.

[1]<http://java.sun.com/javase/6/docs/api/java/lang/Class.html>
[2]<http://sites.google.com/site/drjohnbmatthews/googleolympiad>
[3]<http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/src/org
/gcs/robot/RCImage.java?revision=64&view=markup>
 
M

Mike Amling

Magnus said:
Hallo,

wie kann man denn auf eine Ressource (File) zugreifen, das im obersten
Package liegt bzw. in einem Package ohne Klassen?

Ich habe versucht, von einer existierenden Klasse nach oben zu wandern
("../"), aber das klappt nicht:

Class c = getClass();
InputStream s = c.getResourceAsStream ("../test.cfg");

Erst bin ich kein Expert. getResourceAsStream ist mir fremd.
Sie haben, zum Beispiel, Class com.xyz.pkg.MeinClass, und Sie suchen
com/xyz/test.cfg vom $CLASSPATH, nicht?

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


Deshalb schlage ich

InputStream s = c.getResourceAsStream("/com/xyz/test.cfg");

vor.

--Michael "Meik" Amling
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top