Clean.java


Below is the syntax highlighted version of Clean.java from §5.1 Formal Languages.


/******************************************************************************
 *  Compilation:  javac Clean.java
 *  Execution:    java Clean in.txt out.txt
 *  Dependencies: In.java
 *
 *  Reads in the text file in.txt and writes it back to the file out.txt,
 *  replacing tabs with 4 spaces and removing and trailing whitespace.
 *
 ******************************************************************************/

public class Clean {

    public static void main(String[] args) {
        In in   = new In(args[0]);
        Out out = new Out(args[1]);
        while (in.hasNextLine()) {
            String s = in.readLine();
            s = s.replaceAll("\\t", "    ");
            s = s.replaceAll("\\s+$", "");
            out.println(s);
        }
    }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.