zoukankan      html  css  js  c++  java
  • java LineNumberReader的使用

    前段时间需要读报表打印,需求是可以从第N行读到第N行,发现百度出来的LineNumberReader方法都不怎么理想
    然后就找到一篇和众百度里脱颖而出的好文章,所以分享之:
     
    从类 的命名来看,貌似LineNumberReader可以支持从任意行读取的功能,并且提供了setLineNumber()的方法,但是这个按照并不能改变文件指针的位置,只是你调用getLineNumber()这个函数时,结果会变。API上面说它并不能改变文件物理的指针位置。看来以后得小心使用。
     

    import java.io.File;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.LineNumberReader;
     
    public class Ex7 {
         public void dataReader(String nameFile, int start, int finish) {
              if (start > finish) {
                   System.out.println("Error start or finish!");
                   return;
              }
              InputStream inputStream = null;
              LineNumberReader reader = null;
              try {
                   inputStream = new FileInputStream(new File(nameFile));
                   reader = new LineNumberReader(
                             new InputStreamReader(inputStream));
                   int lines = getTotalLines(new File(nameFile));
                   if (start < 0 || finish < 0 || finish > lines || start > lines) {
                        System.out.println("Line not found!");
                        return;
                   }
                  
                   String line = reader.readLine();
                 lines = 0;
                 while (line != null) {
                     lines++;
                     if(lines >= start && lines <= finish){
                          System.out.println(line);
                     }
                     line = reader.readLine();
                 }
                 inputStream.close();
                   reader.close();
              } catch (FileNotFoundException e) {
                   e.printStackTrace();
              } catch (IOException e) {
                   System.err.println("IO Error");
                   System.exit(0);
              }
     
         }
        
         private int getTotalLines(File file) throws IOException{
              FileReader in = new FileReader(file);
            LineNumberReader reader = new LineNumberReader(in);
            String line = reader.readLine();
            int lines = 0;
            while (line != null) {
                lines++;
                line = reader.readLine();
            }
            reader.close();
            in.close();
            return lines;
         }
     
         public static void main(String[] args) {
              new Ex7().dataReader("data.txt", 2, 4);
              new Ex7().dataReader("data.txt",3,8);
         }
    }
    github https://github.com/hano7758
  • 相关阅读:
    AdminLTE组件之表格DataTable
    爬虫:通过滑动或者点触验证码的方法及实现(点触+滑动)
    爬虫:滑动验证解决方法及python实现
    django文件上传地址以及media的设置
    基于cropper和sweetalert的简单图片/头像裁剪上传
    学写网站(二)前端配置之glup
    轩辕剑陆和外传平台版设置功能
    植物大战僵尸
    仙剑类更新
    VSCode注册关联自定义类型文件
  • 原文地址:https://www.cnblogs.com/jokerSun/p/4172833.html
Copyright © 2011-2022 走看看