开发者

Why getClass() used in the piece of code?

开发者 https://www.devze.com 2023-01-07 03:02 出处:网络
I am trying to access the audio file from the jar,for doing that i wrote a code,like ... try { InputStream is =

I am trying to access the audio file from the jar,for doing that i wrote a code,like

...
try {
   InputStream is = 
     getClass().getResourceAsStream("audio.wav");
   Player player = Manager.createPlayer(is, "audio/X-wav");
   p.start();
} 
catch(IOException ioe) {
} 
catch(MediaException me) {
}
...

but here开发者_如何转开发 in this snippet,what is the use of getClass()?


It allows you to get the class of the current object, so in turn you can access resources. getResourceAsStream loads resources from the class loader associated with the class. See the documentation for Object.getClass and Class.getResourceAsStream.

0

精彩评论

暂无评论...
验证码 换一张
取 消