Java-source code used to search for the number of times a string appears in a file

Source: Internet
Author: User

Import Java. io. bufferedreader; <br/> Import Java. io. file; <br/> Import Java. io. filereader; <br/> Import Java. io. ioexception; <br/>/** <br/> * obtain the number of times a string appears in the file <br/> * @ author lichaoever <br/> */< br/> Public Class search {<br/> file = new file ("/usr/test.txt "); </P> <p> private string buffer; <br/> Public Search () {<br/> try {<br/> system. out. println (this. research ("searchsomething"); <br/>} Cat CH (ioexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}</P> <p >}< br/> Public int Research (string find_str) throws ioexception <br/>{< br/> filereader reader = new filereader (File); <br/> bufferedreader reader2 = new bufferedreader (Reader ); <br/> string S = ""; <br/> DO <br/> {<br/> buffer + = s; <br/>} while (S = reader2.readline ())! = NULL); </P> <p> return buffer. split (find_str ). length-1; <br/>}< br/> Public static void main (string [] ARGs) {<br/> new search (); <br/>}< br/>}

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.