com.darwinsys.io.RevLines Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of darwinsys-api Show documentation
Show all versions of darwinsys-api Show documentation
Ian Darwin's assorted Java stuff,
assembled as an API.
package com.darwinsys.io;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Stack;
/**
* Reverse a file by lines (simple algorithm, keeps file in memory).
*/
public class RevLines {
public static void main(String[] argv) throws Throwable {
String line;
try (BufferedReader is = new BufferedReader(new FileReader(argv[0]))) {
Stack myStack = new Stack();
// Put it in the stack frontwards
while ((line = is.readLine()) != null) {
myStack.push(line);
}
// Print the stack backwards
while (!myStack.empty()) {
System.out.println(myStack.pop());
}
}
}
}