/****************************************************************************** * This program copies the first few and last few lines of stdin to stdout * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import org.pureprogrammer.Utils; public class HeadAndTail { public static void main(String[] args) { int maxLines = 10; if (args.length == 1) { maxLines = Utils.stoiWithDefault(args[0], 10); } List buffer = new ArrayList<>(Collections.nCopies(maxLines, null)); String line; int count = 0; while ((line = Utils.getline()) != null) { buffer.set(count % maxLines, line); if (count < maxLines) { System.out.println(line); } ++count; } if (count > maxLines && count <= 2 * maxLines) { final int REMAINING = count - maxLines; for (int i = 0; i < REMAINING; ++i) { System.out.println(buffer.get(i)); } } else if (count > 2 * maxLines) { System.out.println("..."); for (int i = 0; i < maxLines; ++i) { System.out.println(buffer.get((count + i) % maxLines)); } } } }