package cn.chenqingcan.cmdtext;

import java.io.*;

import cn.chenqingcan.util.*;

/**
 * 看文件开头几行
 * @author 陈庆灿
 */
public class Head {

	//------------------------------------------------------------------------
	/** 程序入口 */
	public static void main(String[] args) {
		TextFileInfo file;
		int lineno;
		try { file = TextFileInfo.prompt(); }
		catch (Throwable e) { return; }

		for (;;) {
			try { lineno = VolcanoConsoleUtil.inputInteger("行数"); }
			catch (Throwable e) { return; }
			try { echoHeadLine(file, lineno); }
			catch (Throwable e) { System.err.println(VolcanoExceptionUtil.throwableToSimpleString(e)); }
		}
	}

	//------------------------------------------------------------------------
	/** 指定行数回显文件开头 */
	private static void echoHeadLine(final TextFileInfo file, final int lineno) throws Throwable {
		try (BufferedReader reader = VolcanoFileUtil.openFileReader(file.getFileName(), file.getCharset())) {
			for (int n = 0 ; n < lineno ; n++) {
				String s = reader.readLine();
				if (s == null) {
					return;
				}
				System.out.println(s);
			}
		}
	}

}
