2010-02-18 32 views
11

Tôi đọc từng dòng của file theo cách dưới đâyLàm thế nào để phát hiện dòng đầu tiên và cuối cùng trong reader.readLine()?

BufferedReader in = new BufferedReader(new FileReader(inFile)); 

while (null != (line = in.readLine())) { 


} 

Tôi muốn làm một số xác nhận trong dòng đầu tiên và dòng cuối cùng một mình. Có cách nào để kiểm tra xem đó là một dòng đầu tiên và dòng cuối cùng bên trong vòng lặp while

while (null != (line = in.readLine())) {  

    if(firstlineoffile) { 
    } 

    else if (lastlineoffile) { 
    } 

    else 
    { 
    } 

} 
+0

Thụt lề mã của bạn bằng 4 không gian để định dạng nó như vậy. Tôi đã sửa nó cho bạn trong bài đăng này. –

Trả lời

11

mát câu hỏi. Tôi chơi một chút nó và đây là một SSCCE, chỉ cần copy'n'paste'n'run nó.

package com.stackoverflow.q2292917; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.PrintWriter; 

public class Test { 

    public static void main(String... args) throws IOException { 
     // Create test file. 
     File file = new File("/test.txt"); 
     PrintWriter writer = new PrintWriter(file); 
     writer.println("line 1"); 
     writer.println("line 2"); 
     writer.println("line 3"); 
     writer.println("line 4"); 
     writer.println("line 5"); 
     writer.close(); 

     // Read test file. 
     BufferedReader reader = null; 
     try { 
      reader = new BufferedReader(new FileReader(file)); 
      String next, line = reader.readLine(); 
      for (boolean first = true, last = (line == null); !last; first = false, line = next) { 
       last = ((next = reader.readLine()) == null); 

       if (first) { 
        System.out.println("first line: " + line); 
       } else if (last) { 
        System.out.println("last line: " + line); 
       } else { 
        System.out.println("normal line: " + line); 
       } 
      } 
     } finally { 
      if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {} 
     } 

     // Delete test file. 
     file.delete(); 
    } 

} 

Output:

 
first line: line 1 
normal line: line 2 
normal line: line 3 
normal line: line 4 
last line: line 5 

tuy nhiên tôi đặt câu hỏi về khả năng đọc và thông dịch bởi người mới bắt đầu ... ;)

+0

Cảm ơn rất nhiều! Đã thử chương trình mẫu và nó đang hoạt động – Arav

+0

Bạn được chào đón. – BalusC

0

nếu bạn thêm một số, và sắp xếp lại mã của bạn một chút, điều này sẽ làm việc (tôi đã không kiểm tra này để có thể có errros cú pháp):

int count = 0; 
String line = in.readLine(); 
while (line!=null) { 
    String currLine = line; 
    if(count==0){ 
    //currLine is the first line 
    } 

    line = in.readLine(); 
    if(line==null){ 
    //currLine is the last line 
    } 

    if(count>0 && line!=null){ 
    //do something with lines in between 
    System.out.println(currLine); 
    } 
    count++; 
} 
+1

Vâng, nếu dòng == null bạn đang vượt qua dòng cuối cùng. Bạn sẽ luôn luôn phải nhìn về phía trước và nếu nextLine == null bạn đang ở cuối cùng. – Kylar

+1

Hoặc bạn có thể cấu trúc lại nó thành một vòng lặp do-while, nó sẽ loại bỏ 'readLine()' bên ngoài vòng lặp. Ngoài ra, nếu dòng == null, dòng _previous_ là dòng cuối cùng, không phải dòng cuối cùng. –

+0

được cấu trúc lại một chút để xác định chính xác dòng cuối cùng – Upgradingdave

0
String currentLine = in.readLine(); 
String nextLine = in.readLine(); 
boolean hasStarted = false; 
while(null != currentLine){ 
    if(!hasStarted){ 
     //first line. 
     hasStarted = true; 
    } 
    //all your processing here. 
    if(null == nextLine){ 
     //last line, cause there's nothing else coming up 
    } 
    currentLine = nextLine; 
    nextLine = in.readLine(); 
} 
-1

có người cũng có thể đưa ra một giải pháp thanh lịch hơn này, nhưng ở đây chúng tôi đi:

boolean isFirstLine = true; 
do{ 
     String line = in.readLine(); 
     if(isFirstLine){ 
     //this is the first line 
     isFirstLine = false; 
     } 
     else if(line==null){ //previous line was the last line 
     in.reset(); 
     line = in.readLine(); 
     //do last line specific stuff 
     break; 
     } 
     else { 
      //do stuff for lines in between 
     } 
     in.mark(100); 

}while (line!=null); 

Tôi chưa thử nghiệm điều này, vì vậy có thể có lỗi nhỏ. Tôi đã không sắp xếp xử lý ngoại lệ trong mã này. readLine(), mark()reset() ném IOExceptionmark() cũng có thể ném IllegalArgumentException

0
public class Vomitfirstline { 

    public static void main(String[] args) { 

     BufferedReader br = null; 

     try { 

      String sCurrentLine; 

      br = new BufferedReader(new FileReader("Path")); 
      br.readLine(); 

      { 


       while ((sCurrentLine = br.readLine()) != null) 
     System.out.println(sCurrentLine); 

      } 

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

bạn có thể kiểm tra mã trên không – Vijay

Các vấn đề liên quan