Search for the Java method of the specified string in a file

Source: Internet
Author: User
Import Java. io. *; import Java. NIO. *; import Java. NIO. channels. *; import Java. NIO. charset. *; public class search {public static void main (string [] ARGs) throws ioexception {If (ARGs. length <2) {system. out. println ("Usage: Java search <File> <string>"); return;} charset = charset. forname ("gb18030"); charsetdecoder decoder = charset. newdecoder (); // if it has nothing to do with Chinese, the two sentences and related statements can be used except string filename = System. Getproperty ("user. dir ") + system. getproperty ("file. separator ") + ARGs [0]; try {fileinputstream FCM = new fileinputstream (filename); filechannel fc = fiis. getchannel (); int SZ = (INT) FC. size (); mappedbytebuffer BB = FC. map (filechannel. mapmode. read_only, 0, SZ); charbuffer cb = decoder. decode (bb); string S = string. valueof (CB); int n = S. indexof (ARGs [1]); If (n>-1) system. out. println (ARGs [1] + "--- "+ N); else system. Out. println (ARGs [1] +" --- not found! "); FC. Close () ;}catch (exception e) {system. Out. println (e );}}}

/*
After compilation, on the command line:

 
Java search file word

File: name of the file to be searched
Word: the word to be searched (string)
*/

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.