001 002 import java.io.*; 003 import java.util.*; 004 005 /** 006 * Exercise 12.3 in the book 007 * 008 * @author Kristian Torp 009 * @version 1.0 010 */ 011 012 public class Exercise12_3 { 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_3.java"; // this file 022 } 023 024 // place to store lines that are read 025 ArrayList al = new ArrayList(); 026 BufferedReader in = null; 027 BufferedWriter out = null; 028 try { 029 // get the input 030 in = new BufferedReader(new FileReader(fileName)); 031 String line; 032 // loop through the lines in the file 033 while ((line = in.readLine()) != null) { 034 al.add(line); 035 } 036 037 // write the output 038 out = new BufferedWriter(new FileWriter(fileName 039 + ".out")); 040 Iterator it = al.iterator(); 041 while (it.hasNext()) { 042 out.write((String) it.next() + "\n"); 043 } 044 out.close(); 045 } catch (Exception e) { 046 System.err.println(e); 047 } finally { 048 if (in != null) { 049 try { 050 in.close(); 051 } catch (IOException e) { 052 System.err.println(e); 053 } 054 } 055 056 if (out != null){ 057 try { 058 in.close(); 059 } catch (IOException e) { 060 System.err.println(e); 061 } 062 } 063 } 064 } 065 }