/** * EnumLines - Annotate input with lines and filenames * Good for testing out new Java installations, and suchlike. * License: GPL 2; share and enjoy! * @author Sean B. Palmer, inamidst.com * @version 0.1 */ package com.inamidst.jmisc; import java.io.BufferedReader; import java.io.FileReader; public class EnumLines { String fn; final String usage = "java com.inamidst.jmisc.EnumLines "; /** * fromFilename - Enumerate a file from its filename. * @param fn Filename to read from. */ public void fromFilename(String fn) { this.fn = fn; try { enumerate(); } catch (Exception e) { barf(e.getMessage()); } } /** Enumerate a file from currently set filename. */ public void enumerate() throws Exception { String line; int n = 0; BufferedReader reader = new BufferedReader(new FileReader(fn)); while ((line = reader.readLine()) != null) { n++; System.out.println(fn + ":" + n + ":" + line); } } /** * Display an error message for a given filename * @param msg Error message */ public void barf(String msg) { System.err.println("E: " + msg + ": " + this.fn); System.exit(1); } /** * Reads filenames from argv and outputs lines annotated * with filename and line. * @param args ARGV[1:]. */ public static void main(String [] args) { EnumLines enum = new EnumLines(); if (args.length != 1) System.out.println("Usage: " + enum.usage); else { enum.fromFilename(args[0]); } } }