2013-01-08 485 views
5

可以说,我有一个名为的文本文件:data.txt中(包含2000行)的Java:如何使用的BufferedReader来读取特定行

如何阅读给定的具体线路由:500-1500然后1500- 2000 并显示特定行的输出?

这个代码将读取整个文件(2000线)

public static String getContents(File aFile) { 

     StringBuffer contents = new StringBuffer(); 

     try { 

     BufferedReader input = new BufferedReader(new FileReader(aFile)); 
     try { 
      String line = null; 

      while ((line = input.readLine()) != null){ 
      contents.append(line); 
      contents.append(System.getProperty("line.separator")); 
      } 
     } 
     finally { 
      input.close(); 
     } 
     } 
      catch (IOException ex){ 
      ex.printStackTrace(); 
     } 

     return contents.toString(); 
} 

如何修改上面的代码读取特定行?

+1

为什么你不只是算哪一行,你是,如果你是在所期望的范围你一次又一次地输出线? – Stefan

+0

我该怎么做?我知道如何计数,但不知道如何输出范围为 – Redbox

+0

的行。您可以计数,然后用'if'语句检查计数。 –

回答

13

我建议java.io.LineNumberReader。它扩展的BufferedReader和 你可以用它LineNumberReader.getLineNumber();来获取当前行号

您也可以使用Java 7 java.nio.file.Files.readAllLines它是否适合你,它返回一个List<String>更好

注:

1)青睐的StringBuilder以上的StringBuffer,StringBuffer的仅仅是一个传统类

2)contents.append(System.getProperty("line.separator"))并不好看 使用contents.append(File.separator)代替

3)捕获的异常似乎无关紧要,我还建议改变你的代码

public static String getContents(File aFile) throws IOException { 
    BufferedReader rdr = new BufferedReader(new FileReader("aFile")); 
    try { 
     StringBuilder sb = new StringBuilder(); 
     // read your lines 
     return sb.toString(); 
    } finally { 
     rdr.close(); 
    } 
} 

现在的代码看起来在我看来,更清洁。如果你是在Java 7中使用try-与资源

try (BufferedReader rdr = new BufferedReader(new FileReader("aFile"))) { 
     StringBuilder sb = new StringBuilder(); 
     // read your lines 
     return sb.toString(); 
    } 

所以最后你的代码可能看起来像

public static String[] getContents(File aFile) throws IOException { 
    try (LineNumberReader rdr = new LineNumberReader(new FileReader(aFile))) { 
     StringBuilder sb1 = new StringBuilder(); 
     StringBuilder sb2 = new StringBuilder(); 
     for (String line = null; (line = rdr.readLine()) != null;) { 
      if (rdr.getLineNumber() >= 1500) { 
       sb2.append(line).append(File.pathSeparatorChar); 
      } else if (rdr.getLineNumber() > 500) { 
       sb1.append(line).append(File.pathSeparatorChar); 
      } 
     } 
     return new String[] { sb1.toString(), sb2.toString() }; 
    } 
} 

注意,它返回两个字符串500-1499和1500-2000

+0

看来Redbox希望500-1500和1500 - 2000作为两个单独的文本,或似乎 –

+0

我想把(500-1500)的文本字符串A 然后文本(1500-2000)作为字符串B 可能做到这一点? – Redbox

+0

然后使用LineNumberReader,它会更有效率 –

4

稍微更清洁的解决方案是在Apache公共中使用FileUtils。 http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html 例片段:

String line = FileUtils.readLines(aFile).get(lineNumber); 
+0

我需要加载apache libs吗?即时通讯使用jdeveloper – Redbox

+0

是的,apache commons-io jar文件应该在classpath中。 http://www.jarfinder.com/index.php/java/info/org.apache.commons.io.FileUtils –

+0

你答案中的链接已经死了 你在这里找到FileUtils:http://commons.apache。组织/合适/公-IO / – ConquerorsHaki

1

的更好的方法是使用的BufferedReader。如果你想读线32例如:

for(int x = 0; x < 32; x++){ 
    buf.readLine(); 
} 
lineThreeTwo = buf.readLine(); 

现在字符串lineThreeTwo已存储在线32

相关问题