import java.util.* ; import java.io.* ; public class FileTest { public static void main(String [] args) { Vector lines = new Vector() ; // Read all lines in "infile" to `lines' try { BufferedReader in = new BufferedReader(new FileReader("infile")) ; while(true) { String line = in.readLine() ; if(line == null) break ; // Normal end of file. line = line.trim() ; // Optionally trim off whitespace lines.addElement(line) ; } } catch (FileNotFoundException e) { ; // OK. Treated as an empty file. } catch (IOException e) { System.out.println("Error loading infile: " + e.getMessage()) ; System.exit(1) ; } // Write all lines in `lines' to "outfile" try { PrintWriter out = new PrintWriter(new FileWriter("outfile")) ; // ... Will create "outfile" if it doesn't exist, // or *overwrite* it if it does (caution!) for(int i = 0 ; i < lines.size() ; i++) { String line = (String) lines.elementAt(i) ; out.println(line) ; } out.close() ; // (Must explicitly close to force buffer flush) } catch (IOException e) { System.out.println("Error writing outfile: " + e.getMessage()) ; System.exit(1) ; } } }