/* When run, this program opens the specified file, prints out it's contents, and then writes them back to the same file in reverse order (last line first) */ import java.io.*; import java.util.*; public class InputOutput { ArrayList contents; public InputOutput() { contents=new ArrayList(); } public static void main(String[] args) { if(args.length!=1) { System.err.println("Usage: InputOutput fileName"); return; } InputOutput io=new InputOutput(); if(!io.readFile(args[0])) return; io.printContents(); if(!io.writeFile(args[0])) return; } public boolean readFile(String fname) { try { BufferedReader br=new BufferedReader(new FileReader(fname)); String s=br.readLine(); while(s!=null) { contents.add(s); s=br.readLine(); } br.close(); } catch(Exception e) { System.err.println("Error reading file '"+fname+"': "+e); return false; } return true; } public void printContents() { for(int i=0;i=0;i--) { bw.write(contents.get(i)); bw.newLine(); } bw.close(); } catch(Exception e) { System.err.println("Error writing file '"+fname+"': "+e); return false; } return true; } }