Java读取文本指定的某一行内容


Java读取文本指定的某一行内容,使用的都是IO的方法,下面具体看例子:

 

[java]  view plain  copy
 
  1. /** 
  2.  * @author:罗大锤 
  3.  * @date: 2017年9月6日 下午2:35:43 
  4.  * @version 1.0 
  5.  * @method:读取文本具体某行内容 
  6.  * @parameter 
  7.  * @since 
  8.  * @return 
  9.  */  
  10. public class OpenTextLine {  
  11.     public static void main(String[] args) throws IOException {  
  12.         long timeStart = System.currentTimeMillis();  
  13.         File file = new File("testData.txt");//文件路径  
  14.         FileReader fileReader = new FileReader(file);  
  15.         LineNumberReader reader = new LineNumberReader(fileReader);  
  16.         int number = 9999999;//设置指定行数  
  17.         String txt = "";  
  18.         int lines = 0;  
  19.         while (txt != null) {  
  20.             lines++;  
  21.             txt = reader.readLine();  
  22.             if (lines == number) {  
  23.                 System.out.println("第" + reader.getLineNumber() + "的内容是:" + txt + "\n");  
  24.                 long timeEnd = System.currentTimeMillis();  
  25.                 System.out.println("总共花费:" + (timeEnd - timeStart) + "ms");  
  26.                 System.exit(0);  
  27.             }  
  28.         }  
  29.         reader.close();  
  30.         fileReader.close();  
  31.     }  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM