1. Write a class inheritance and filefilter
Package COM. dream. musicplayer; </P> <p> Import Java. io. file; <br/> Import Java. io. filefilter; </P> <p> public class mp3filefilter implements filefilter {</P> <p> @ override <br/> Public Boolean accept (File file) {<br/> // todo auto-generated method stub <br/> // return false; </P> <p> If (file. isdirectory () <br/> return true; <br/> else <br/>{< br/> string name = file. getname (); <br/> If (name. endswith (". MP3 ") | Name. endswith (". MP4 ") <br/> return true; <br/> else <br/> return false; <br/>}</P> <p >}< br/>
2. Upload a path to obtain all MP3 and MP4 files in the modified path.
/** <Br/> * Get all the music file in the rootpath. <br/> * @ Param rootpath <br/> */<br/> Public void getallfilepath (string rootpath) <br/>{</P> <p> file = new file (rootpath); <br/> file [] files = file. listfiles (New mp3filefilter (); <br/> for (INT I = 0; I <files. length; I ++) <br/>{< br/> If (files [I]. isdirectory () <br/>{< br/> getallfilepath (files [I]. getpath (); <br/>}< br/> else <br/>{< br/> marraylistmusicpaths. add (files [I]. getpath (); <br/> marraylistmusicnames. add (files [I]. getname (); <br/> system. out. println (files [I]. getpath (); <br/>}</P> <p>}
In this way, you can obtain all the file types in a path.