Advertisement
fayimora

Print the contents of a file in the reverse order

Jun 28th, 2011
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class ReverseParagraphs
  5. {
  6.     public static void main(String[] args) {
  7.         ArrayList<String> lines = new ArrayList<String>();
  8.         try{
  9.             readInLines(lines);
  10.             printOutLines(lines);
  11.         }catch(IOException e){
  12.             System.out.println(e);
  13.         }
  14.         System.exit(0);
  15.     }
  16.     static void readInLines(ArrayList<String> lines) throws IOException {
  17.         BufferedReader buffer = new BufferedReader(new FileReader("lines.txt"));
  18.         while(buffer.ready())
  19.             lines.add(buffer.readLine());
  20.     }
  21.     static void printOutLines(ArrayList<String> lines) {
  22.         for(int i=lines.size()-1; i>=0; i--)
  23.             System.out.println(lines.get(i));
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement