Java file filtering filefilter

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.