个人博客:skyfffire.cn

列出一个文件的内容(带行号)——《Thinking in Java》随笔029

 1 //: TestEOF.java
 2 package c10;
 3 
 4 import java.io.FileInputStream;
 5 import java.io.InputStreamReader;
 6 
 7 /**
 8 *    @time:         上午9:55:40
 9 *    @date:         2017年4月30日
10 *    @auther:    skyfffire
11 *    @version:    v0.1
12 */
13 public class TestEOF {
14     public static void main(String[] args) {
15         try {
16             String path = "./src/c10/TestEOF.java";
17             String encoding = "UTF-8";
18             
19             InputStreamReader isr = new InputStreamReader(
20                     new FileInputStream(path), encoding);
21             int c = 0;
22             int lineNumber = 1;
23             
24             System.out.format("%-4d| ", lineNumber++);
25             while ( (c = isr.read()) != -1) {
26                 System.out.print((char) c);
27                 
28                 if (c == '\n') {
29                     System.out.format("%-4d| ", lineNumber++);
30                 }
31             }
32             
33             isr.close();
34         } catch (Exception e) {
35             e.printStackTrace();
36         }
37     }
38 }
39 
40 ///:

 

posted @ 2017-04-30 10:32  skyfffire  阅读(176)  评论(0编辑  收藏  举报
个人博客:skyfffire.cn