Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.util.regex.Pattern;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. /*
  13.  * Grep.java
  14.  *
  15.  * Copyright (c) 2020
  16.  * All Rights Reserved.
  17.  *
  18.  * @version 1.00 - 24 Mar 2020 - HS - initial version
  19.  */
  20.  
  21. /**
  22.  * A simple grep implementation.
  23.  *
  24.  * @author Hendrik Schlange, mail@hendrikschlange.de
  25.  *
  26.  */
  27.  
  28. public class Grep {
  29.  
  30.     static String lineContent;
  31.  
  32.     /**
  33.      *
  34.      * @param args list of arguments transferred from the operating system
  35.      * args[0]: the substring to find in a line, as regular expression.
  36.      * args[1]: the file to read from. OPTIONAL, default: standard input
  37.      * args[2]: the file to write to. OPTIONAL, default: standard output
  38.      */
  39.     public static void main(java.lang.String[] args) {
  40.         if (args.length < 1 || args.length > 3) {
  41.             System.out.println("usage: Grep PATTERN [from-file] [to-file]");
  42.             System.exit(1);
  43.         } else {
  44.             // Compiling the regex to match the whole line when searching for the string in args[0]
  45.             String regex = ".*" + args[0] + ".*";
  46.  
  47.             // First assumption: using stdin and stdout
  48.             InputStreamReader inputStreamReader = new InputStreamReader(
  49.                     new ByteArrayInputStream(args[1].getBytes()));
  50.             OutputStreamWriter outputStreamWriter = new OutputStreamWriter(System.out);
  51.  
  52.             /*
  53.              * Opening the input.
  54.              * Using args[1] as file input.
  55.              */
  56.             try {
  57.                 inputStreamReader = new InputStreamReader(new FileInputStream(args[1]));
  58.             } catch (FileNotFoundException in) {
  59.                 // In this case, the file could not be opened
  60.                 // and we want the InputStream to remain coming from args[1].
  61.                 //System.out.println(in);
  62.             }
  63.             // Initialize outputStreamWriter if a filename for the output has been provided
  64.             if(args.length > 2) {
  65.                 try {
  66.                     outputStreamWriter = new OutputStreamWriter(
  67.                             new FileOutputStream(args[2]));
  68.                 } catch (Exception out) {
  69.                     System.out.println("No output possible");
  70.                 }
  71.             }
  72.  
  73.             // Calling filter function to start the actual grep
  74.             filter(regex, inputStreamReader, outputStreamWriter);
  75.         }
  76.     }
  77.  
  78.  
  79.     /**
  80.      * Copies all lines containing the search string from the Reader to the Writer.
  81.      * @param regex the search string as regular expression
  82.      * @param reader the input stream
  83.      * @param writer the output stream
  84.      */
  85.     public static void filter (String regex, java.io.Reader reader, java.io.Writer writer) {
  86.         BufferedReader bufferedReader = new BufferedReader(reader);
  87.         BufferedWriter bufferedWriter = new BufferedWriter(writer);
  88.         // While the reader is ready, read each line and compare it to the pattern.
  89.         try {
  90.             while (bufferedReader.ready()) {
  91.                 lineContent = bufferedReader.readLine();
  92.                 // If the pattern matches, write is to the file provided in args[2]
  93.                 if(Pattern.matches(regex, lineContent)) {
  94.                     bufferedWriter.write(lineContent);
  95.                     bufferedWriter.newLine();
  96.                 } else {
  97.                     continue;
  98.                 }
  99.             }
  100.             bufferedReader.close();
  101.             bufferedWriter.close();
  102.         } catch (IOException e) {
  103.             // Could not write from the input
  104.             e.printStackTrace();
  105.         }
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement