2013-07-25 18 views
14

Bir metin dosyasından belirli satır numaralarını almanın en etkili yolu nedir? Örneğin, tarayıcıyı bir dosyayı ayrıştırmak için kullanırsam, önce metin dosyasındaki toplam satır sayısıyla eşleşen bir dizi oluşturmak zorunda mıyım?Java'da bir metin dosyasından belirli bir satırı okuma

Bir metin dosyası 30 satıra sahipse ve yalnızca 3, 8 ve 12 numaralı satırlarla çalışmak istiyorsam, yalnızca bu satırları okumak için bir yol var mı? Burada

+7

her satırın uzunluğu bütünlüğü konusunda emin değilseniz sonra döngü muhtemelen bir sayacı kullanılarak! – NINCOMPOOP

+0

http://stackoverflow.com/questions/2312756/in-java-how-to-read-from-a-file-a-specific-line-given-the-line-number –

+1

Müthiş. Herkese teşekkürler. Galiba sayaç değişkeni kullanacağım. Mantıklı. – Jon

cevap

7

bunu nasıl geçerli:

burada
import java.io.*; 

public class Test { 
public static void main(String [] args) { 

    // The name of the file to open. 
    String fileName = "temp.txt"; 
    int counter = 0; 

    // This will reference one line at a time 
    String line = null; 
    FileReader fileReader = null; 

    try { 
     // FileReader reads text files in the default encoding. 
     fileReader = 
      new FileReader(fileName); 

     // Always wrap FileReader in BufferedReader. 
     BufferedReader bufferedReader = 
      new BufferedReader(fileReader); 

     while((line = bufferedReader.readLine()) != null) { 
      counter++; 
      if(counter == 3 || counter == 8 || counter == 12) 
      { 
       // do your code 
      } 
     } 

    } 
    catch(FileNotFoundException ex) { 
     System.out.println(
      "Unable to open file '" + 
      fileName + "'");     
    } 
    catch(IOException ex) { 
     System.out.println(
      "Error reading file '" 
      + fileName + "'");     
     // Or we could just do this: 
     // ex.printStackTrace(); 
    } 
    finally 
    { 
     if(fileReader != null){ 
      // Always close files. 
      bufferedReader.close();    
     } 
    } 
} 
} 
+1

Sadece nihayetinde beni yenmek için – MadProgrammer

+0

+1 açık bir istisna olmadığından emin olmak için "nihayet" bloğunda akışlarınızı kapatmalısınız. – MaVRoSCy

+0

@MadProgrammer true, düzeltildi. Teşekkürler –

4

yapabileceğiniz şeydir. (Bu programın sadece bir parçası değil, tam olarak programdır)

int counter 0 =; 
BufferedReader br = new BufferedReader(new FileReader(file)); 
String line; 
while ((line = br.readLine()) != null) { 
    // process the line. 
    counter++; 

    switch(counter){ 
    case 3: 
     \\ do your code for line no 3 
     break; 
    case 8: 
     \\ do your code for line no 8 
     break; 
    case 12: 
     \\ do your code for line no 12 
     break; 
    } 
}  
br.close(); 
3

son soruya gelince bu

try 
    { 
     String sCurrentLine; 
     br = new BufferedReader(new FileReader("File_Path")); 
     int counter = 0; 
     while ((sCurrentLine = br.readLine()) != null) 
     { 
      counter++; 
      if (counter == 3 || counter == 8 || counter == 12) 
      { 
       System.out.println("" + br.read()); 
       if (counter == 12) 
        break; 
      } 
     } 
    } 
    catch (IOException e) 
    { 
     System.out.println("Exception " + e); 

    } 
    finally 
    { 
     try 
     { 
      if (br != null) 
      { 
       br.close(); 
      } 
     } 
     catch (IOException ex) 
     { 
      ex.printStackTrace(); 
     } 
    } 
1

deneyin: Bu answer göre, Java 8 belirli satırları ayıklamak sağlar dosya. Bu cevapta örnekler verilmiştir.

İlgili konular