📄 wordsearchexample.java
字号:
import java.util.StringTokenizer;import java.io.*;public class WordSearchExample{ private String fileName = null; // Default Constructor public WordSearchExample( String fileName ) { super(); this.fileName = fileName; } // Private Accessor private String getFilename() { return fileName; } // Method that actually performs the search public int doSearch( String word ) { LineNumberReader reader = null; int count = 0; StringTokenizer tokenizer = null; String currentString = null; String tempString = null; try { reader = new LineNumberReader( new FileReader( getFilename() ) ); while( (currentString = reader.readLine()) != null ) { // No sense tokenizing an empty string if ( currentString.equals( "" ) ) continue; System.out.println( "Searching: " + currentString ); tokenizer = new StringTokenizer( currentString ); while( tokenizer.hasMoreTokens() ) { tempString = tokenizer.nextToken(); if ( !tempString.equals("") && tempString.equals( word )) { System.out.println( "Found on line " + reader.getLineNumber() + ": " + currentString ); System.out.println( "\n" ); count++; } } } } catch( IOException ex ) { System.out.println( "Problem locating or opening the file: " + getFilename() ); } // return the number of instances that were found return count; } // Main Method public static void main( String[] args ) { if ( args.length != 2 ) { System.out.println( "Usage: java WordSearchExample <Filename>" ); System.exit( 0 ); } // Get the strings passed in from the command line String fileName = args[0]; String word = args[1]; // Create an instance of the Example Class WordSearchExample example = new WordSearchExample( args[0] ); int count = example.doSearch( word ); System.out.println ( count + " instances of the word: " + word + " found!" ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -