Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

Loading an external file as resource

Thread view: 
Allan Bruce - 15 Jan 2006 15:27 GMT
I want to be able to play small audio files from my apllication but I am
having problems locating the files.  I want this to be successful whether I
am running from the class files or a jar file.  What is the best way to do
this?  I am currently trying to load the files using this method:

URL lFileURL = AudioPlayer.class.getResource("/Sounds/" + xiFile);

but it doesnt seem to work.
Thanks
Allan
opalpa@gmail.com opalinski from opalpaweb - 15 Jan 2006 17:06 GMT
Put the Sounds directory inside the directory holding AudioPlayer class
then access it with getResource("Sounds/"+xiFile)

This will work from jars or outside of jars.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
opalpa@gmail.com opalinski from opalpaweb - 15 Jan 2006 17:12 GMT
Also consider replacing the "/" with java.io.File.separator for cross
platform-ness.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Roedy Green - 15 Jan 2006 23:04 GMT
On 15 Jan 2006 09:12:37 -0800, "opalpa@gmail.com opalinski from
opalpaweb" <opalpa@gmail.com> wrote, quoted or indirectly quoted
someone who said :

>Also consider replacing the "/" with java.io.File.separator for cross
>platform-ness.

I know that both / and \\ work on windows.  Is it true that / is safe
for all platforms?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

opalpa@gmail.com opalinski from opalpaweb - 15 Jan 2006 23:34 GMT
Looked into this a little.  Class.getResource passes work off to
ClassLoader.getResource which passes it off beyond sources available to
me.  I looked at ClassLoader.getResource documentation and to my
suprise it said that the path passed in is to be "/" seperated because
these things are done via URL and not file path.  Here is link to
documentation:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ClassLoader.html#getResource(j
ava.lang.String
)

I believe this means my second email recommeding File's separator is
incorrect and that "/" is to be used according to documentaiton.

Thanks for pointing to this Roedy; I wouldn't have given it more
thought without your message.

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Roedy Green - 16 Jan 2006 00:06 GMT
On 15 Jan 2006 15:34:38 -0800, "opalpa@gmail.com opalinski from
opalpaweb" <opalpa@gmail.com> wrote, quoted or indirectly quoted
someone who said :

>Thanks for pointing to this Roedy; I wouldn't have given it more
>thought without your message.

I will reask the question in terms of new File( ).  Will it take "/"
on every platform?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Allan Bruce - 16 Jan 2006 00:54 GMT
> On 15 Jan 2006 15:34:38 -0800, "opalpa@gmail.com opalinski from
> opalpaweb" <opalpa@gmail.com> wrote, quoted or indirectly quoted
[quoted text clipped - 5 lines]
> I will reask the question in terms of new File( ).  Will it take "/"
> on every platform?

From what I know, yes that should work.  I remember reading something
stating this but cant for the life of me find it now...
Allan


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.