Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect first and last line during reader.readLine()?

Tags:

java

file

I am reading each line of the file in the below way

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

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


}

I want to do some validation in the first line and last line alone. Is there any way to check if it's a first line and last line inside the while loop

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

    if(firstlineoffile) {
    }

    else if (lastlineoffile) {
    }

    else
    {
    }

}
like image 767
Arav Avatar asked Feb 18 '10 23:02

Arav


People also ask

How do I get the first and last line of a file in Python?

Use file. readline() to read a single line from a file readline() to get the first line of the file and store this in a variable first_line . Create a second variable, last_line , and iterate through all lines in the file until the end. Once the iteration ends, last_line will contain the last line of the file.

Does readLine go to the next line?

readline() returns the next line of the file which contains a newline character in the end. Also, if the end of the file is reached, it will return an empty string.

Does BufferedReader read line by line?

Java Read File line by line using BufferedReaderWe can use java. io. BufferedReader readLine() method to read file line by line to String. This method returns null when end of file is reached.


1 Answers

Cool question. I played a bit round it and here's an SSCCE, just copy'n'paste'n'run it.

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

I however question the readability and interpretability by starters... ;)

like image 53
BalusC Avatar answered Sep 25 '22 03:09

BalusC