码迷,mamicode.com
首页 > 编程语言 > 详细

java I/O系统 LineNumberReader类

时间:2019-02-28 18:37:52      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:exit   读取文件   方便   package   number   main   lis   文件   while   

LineNumbeReader类可以很方便的读取文件的行号

package ch13;
import java.io.*;

import io.BufferedInputFile;

public class BasicFileOutput {
  static String file = "src/ch13/Writer.java";
  public static void main(String[] args)
          throws IOException {
            BufferedReader in = new BufferedReader(
              new StringReader(
                BufferedInputFile.read("src/ch13/Read.java")));
            PrintWriter out = new PrintWriter(
              new BufferedWriter(new FileWriter(file)));
            LineNumberReader ln = new LineNumberReader(in);
            int lineCount = 1;
            String s;
            ln.setLineNumber(5);
            while((s = ln.readLine()) != null )
              out.println(ln.getLineNumber() + ": "+ s);
            out.close();
            // Show the stored file:
            System.out.println(BufferedInputFile.read(file));
          }
} /* 6: package ch13;
7: 
8: import java.io.*;
9: import java.util.Iterator;
10: import java.util.LinkedList;
11: import java.util.ListIterator;
12: 
13: public class Read {
14:     public static void main(String[] args)throws IOException
15:     {
16:         if(args.length!=1)
17:             System.exit(1);
18:         BufferedReader in = new BufferedReader(new FileReader(args[0]));
19:         LinkedList<String> list = new LinkedList<String>();
20:         String s;
21:         StringBuilder sd = new StringBuilder();
22:         while(in.ready())
23:         {
24:             s=in.readLine();
25:             list.add(s);
26:         }
27:         PrintWriter out = new PrintWriter(
28:                 new BufferedWriter(new FileWriter("../src/ch12/Writer.java")));
29:         Integer lineNumber = 1;
30:         for(String str:list)
31:         {
32:             out.println((lineNumber++) + ": " + str);
33:         }
34:         out.close();
35:     }
36: }

 *///:~

 

java I/O系统 LineNumberReader类

标签:exit   读取文件   方便   package   number   main   lis   文件   while   

原文地址:https://www.cnblogs.com/jiangfeilong/p/10451892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!