All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.darwinsys.io.RevLines Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
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());
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy