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)
*/