Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.ByteArrayInputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.util.regex.Pattern;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- /*
- * Grep.java
- *
- * Copyright (c) 2020
- * All Rights Reserved.
- *
- * @version 1.00 - 24 Mar 2020 - HS - initial version
- */
- /**
- * A simple grep implementation.
- *
- * @author Hendrik Schlange, mail@hendrikschlange.de
- *
- */
- public class Grep {
- static String lineContent;
- /**
- *
- * @param args list of arguments transferred from the operating system
- * args[0]: the substring to find in a line, as regular expression.
- * args[1]: the file to read from. OPTIONAL, default: standard input
- * args[2]: the file to write to. OPTIONAL, default: standard output
- */
- public static void main(java.lang.String[] args) {
- if (args.length < 1 || args.length > 3) {
- System.out.println("usage: Grep PATTERN [from-file] [to-file]");
- System.exit(1);
- } else {
- // Compiling the regex to match the whole line when searching for the string in args[0]
- String regex = ".*" + args[0] + ".*";
- // First assumption: using stdin and stdout
- InputStreamReader inputStreamReader = new InputStreamReader(
- new ByteArrayInputStream(args[1].getBytes()));
- OutputStreamWriter outputStreamWriter = new OutputStreamWriter(System.out);
- /*
- * Opening the input.
- * Using args[1] as file input.
- */
- try {
- inputStreamReader = new InputStreamReader(new FileInputStream(args[1]));
- } catch (FileNotFoundException in) {
- // In this case, the file could not be opened
- // and we want the InputStream to remain coming from args[1].
- //System.out.println(in);
- }
- // Initialize outputStreamWriter if a filename for the output has been provided
- if(args.length > 2) {
- try {
- outputStreamWriter = new OutputStreamWriter(
- new FileOutputStream(args[2]));
- } catch (Exception out) {
- System.out.println("No output possible");
- }
- }
- // Calling filter function to start the actual grep
- filter(regex, inputStreamReader, outputStreamWriter);
- }
- }
- /**
- * Copies all lines containing the search string from the Reader to the Writer.
- * @param regex the search string as regular expression
- * @param reader the input stream
- * @param writer the output stream
- */
- public static void filter (String regex, java.io.Reader reader, java.io.Writer writer) {
- BufferedReader bufferedReader = new BufferedReader(reader);
- BufferedWriter bufferedWriter = new BufferedWriter(writer);
- // While the reader is ready, read each line and compare it to the pattern.
- try {
- while (bufferedReader.ready()) {
- lineContent = bufferedReader.readLine();
- // If the pattern matches, write is to the file provided in args[2]
- if(Pattern.matches(regex, lineContent)) {
- bufferedWriter.write(lineContent);
- bufferedWriter.newLine();
- } else {
- continue;
- }
- }
- bufferedReader.close();
- bufferedWriter.close();
- } catch (IOException e) {
- // Could not write from the input
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement