grep.java

来自「这个文件里面包含了java设计模式的一些例子讲解」· Java 代码 · 共 41 行

JAVA
41
字号
package com.javapatterns.decorator.greps;

import java.io.*;

// This class demonstrates the use of the GrepInputStream class.
// It prints the lines of a file that contain a specified substring.
public class Grep
{
    public static void main(String[] args)
    {
        if ((args.length == 0) || (args.length > 2))
        {
            System.out.println("Usage: java Grep <substring> [<filename>]");
            System.exit(0);
        }
        
        try
        {
            DataInputStream d;
            if (args.length == 2) 
                d = new DataInputStream(new FileInputStream(args[1]));
            else
                d = new DataInputStream(System.in);
            
            GrepInputStream g = new GrepInputStream(d, args[0]);
            
            String line;
            for(;;)
            {
                line = g.readLine();
                if (line == null) break;
                System.out.println(line);
            }
            g.close();
        }
        catch (IOException e) { System.err.println(e); }
    }
}


⌨️ 快捷键说明

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