2013-01-08 45 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

相關問題