⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grep.java

📁 java与模式 一书的源码
💻 JAVA
字号:
package com.javapatterns.decorator.grepr;

import java.io.*;

/** This class implements a subset of the UNIX tool grep and
 ** demonstrates the use of the GrepInputStream class.
 ** It prints the filename, linenumbers and lines of the file that 
 ** contains a specified substring (no regexp)
 ** @see GrepInputStream
 ** @author Ulrik Schroeder
 ** @version Joop97-Demo
 **/
public class Grep {
	static GrepReader g;		// own FilterReader definition

    private static GrepView gv = new GrepView();
	/** reads substring and files to be looked through from commandline.
	 ** Unfortunately DOS shell does not expand wildcards when calling Grep.
	 ** @param args command line arguments: 1 substring, 2 .. n Filenames
	 ** @see <{DataInputStream}>
	 ** @see GrepInputStream 
	 ** @see GrepInputStream#GrepInputStream 
	 ** @see GrepInputStream#line 
	 ** @see GrepInputStream#lineNo 
	 **/
	public static void main( String[] args  )
    {
	
        if ( args.length <= 1 )
        {
            gv.println("Usage: java Grep <substring> <filenames>");
            gv.println("       <substring> no regexp");
            gv.println("       <filenames> files to be searched in");
            System.exit( 1 );
        }
        
		String line;
        try
        {
            gv.println( "\nGrep: 搜索 " + args[0] + " 文件 " + args[1] );
            gv.println( "文件行号\t\t 下面的行里含有所搜索的字符串\n" );
//  		gv.println( "\n--> Grep: searching " + args[ 0 ] + " in " + args[ i ] + " <----------" );
//			gv.println( "File:Line:\t Line containing substring\n" );
			g = new GrepReader( new FileReader( args[1] ), args[0] );
			for( ; ; )
               {
				line = g.readLine( );
				if (line == null) break;
				gv.println( args[1] + g.lineNo( ) + ":\t" + line );
			} // loop through lines of file
			g.close( );
        } // try
		catch ( IOException e ) { 
			gv.println( e.getMessage() );
		} // catch IOException 
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -