import java.io.IOException; import java.io.PrintStream; /** * Example of writing to System.out or to a file with the same function! * * @author geoffreytowell */ public class Outputter { private String[] sLine; // just something to print public Outputter(String l) { this.sLine=l.split(" "); } /** * Without args, write to System.out */ public void outt() { outt(System.out); } /** * One printstream arg, write to that printstream * @param ps the printstream to which to write */ public void outt(PrintStream ps) { for (String s: sLine) ps.println(s); } /** * One string arg, open a printstream to write to that named file * @param filename the file to whcih to write */ public void outt(String filename) { try (PrintStream fileWriter = new PrintStream(filename);) { outt(fileWriter); } catch (IOException ioe) { System.err.println(ioe.toString()); } } public static void main(String[] args) { Outputter oo = new Outputter("The quick brown fox junps over the lazy dog"); oo.outt(); oo.outt(System.out); oo.outt(System.err); oo.outt("outfile.txt"); } /*** * java Main -f sydney -m sydney /home/gtowell/Public206/a4/names1995.csv 1995 Sydney Girls Alpha Rank: 78 of 1000 Years: [1995] Percentage: 0.5093187048990258 java Main -f sydney -m sydney /home/gtowell/Public206/a4/names1995.csv /home/gtowell/Public206/a4/names1996.csv /gtowell/Public206/a4/names1996.csv 1995 1996 Sydney Girls Alpha Rank: 87 of 1060 Years: [1995, 1996] Percentage: 0.5080768789563149 Sydney Boys Alpha Rank: 100 of 1048 Years: [1996] Percentage: 0.0037155901518283025 sfeng */ }