日期:2014-05-20 浏览次数:20685 次
/* * 打印一个给定的TXT文件的后20行 */ public static void readTxt() { Map dataMap = new HashMap(); FileReader file; int num = 0; try { file = new FileReader("D:/data.txt"); BufferedReader in = new BufferedReader(file); while (in.ready()) { // 读入每行数据,dataMap中始终只会保存最后读到的20条记录 dataMap.put((num % 20), in.readLine()); num++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (num < 20) { // 文件中记录不超过20行 for (int i = 0; i < num; ++i) { System.out.println(dataMap.get(i)); } } else { int key = num % 20; for (int i = key; i < 20; ++i) { System.out.println(dataMap.get(i)); } for (int i = 0; i < key; ++i) { System.out.println(dataMap.get(i)); } } }