2011-12-07 17 views
1

スキャナを使用してファイルを1行ずつ読み込みます。だから私の質問は、次の行に移動し続けると、前の行を並べて保存する方法です。ファイルを1行ずつ読み取って前の行を取得する方法

いずれかの提案があります。私は前の行がない場合、その後、あなたはifブロックを入力したくないだろうと仮定してい

答えて

4
String previousLine = null; 
while (scanner.hasNextLine()) { 
    String line = scanner.nextLine(); 
    if (line.contains("path=/select")) { 
     // use previousLine here (null for the first iteration or course) 
    } 
    previousLine = line; 
} 
+0

+1私をマークに打つため。 – AusCBloke

1
prevLine = null; // null indicates no previous lines 

while (scanner.hasNextLine()) { 
    line = scanner.nextLine(); //this will get the current line 
    if (line.contains("path=/select") && prevLine != null){ 
     // ... 
    } 
    prevLine = line; 
} 

+0

'line!= null'のテストは完全に無意味です。なぜあなたが理解できたら参照してください。 – Bohemian

+0

@Bohemian:コードの下のテキストに対応する 'prevLine'とされていたwoops ...: – AusCBloke

関連する問題