001 002 import java.io.*; 003 import java.util.*; 004 005 /** 006 * Exercise 12.4 in the book 007 * 008 * @author Kristian Torp 009 * @version 1.0 010 */ 011 012 public class Exercise12_4 { 013 014 public static void main(String[] args) { 015 String fileName; 016 017 // use command line argument if exists 018 if (args.length > 0) { 019 fileName = args[0]; 020 } else { 021 fileName = "Exercise12_4.java"; // this file 022 } 023 024 // place to store lines that are read 025 ArrayList al = new ArrayList(); 026 BufferedReader in = null; 027 try { 028 // get the input 029 in = new BufferedReader(new FileReader(fileName)); 030 String line; 031 // loop through the lines in the file 032 while ((line = in.readLine()) != null) { 033 al.add(line); 034 } 035 036 // write the output 037 Iterator it = al.iterator(); 038 while (it.hasNext()) { 039 line = (String) it.next(); 040 line = line.toUpperCase(); 041 System.out.println(line); 042 } 043 } catch (Exception e) { 044 System.err.println(e); 045 } finally { 046 // close the file 047 if (in != null) { 048 try { 049 in.close(); 050 } catch (IOException e) { 051 System.err.println(e); 052 } 053 } 054 } 055 056 } 057 }