Read first line and last line of a file

Hi everyone,

I would like to have some validation on the first and last line of a file.Currently what I had coded is as follow.


String strLine = null;
FileInputStream fis = new FileInputStream(inputFile);
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
LineNumberReader ln = new LineNumberReader(br);
Scanner sn= new Scanner(fis);

while ((strLine = br.readLine()) != null && ln.getLineNumber() == 0) {
        if (strLine.startsWith("H") && ln.getLineNumber() == 0){ 
          //first line checking, throw exception
        }
       if (strLine.startsWith("T") && !sn.hasNextLine())  {
           //last line checking,throw exception
       }
}

I added the LineNumberReader and Scanner condition but it seem like not working,the checking code never being executed.

If I removed them, only the first line is checked. Let say the file has 5 lines. If first line not started as ‘H’, hence exception thrown which is correct. But if third line started as ‘T’, it will still throw exception which not suppose to happen.

Any way can revise above code better to handle the validation correctly?

hey, I’ve never seen the Scanner class before… interesting :slight_smile:

you could loop using this:

        int lineNumber = 0;
        while (sn.hasNextLine())
        {
            String line = sn.nextLine();
            System.out.println(lineNumber + ":" + line +":" + !sn.hasNextLine());
            lineNumber ++;
        }

hope it helps

Thanks jurn.If I have 5 lines, then the loop will print out:

0:line 2:1
1:line 3:1
2:line 4:1
3:line 5:0

It seem not mean anything, I just need to validate the last line as per the condition in my second if statement. The BufferReader is used to traverse the whole file line by line and !sn.hasNextLine() is applied to detect which line comes last. However, the program seem not getting the correct ‘last line’ in validation.


String strLine = null;
FileInputStream fis = new FileInputStream(inputFile);
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
LineNumberReader ln = new LineNumberReader(br);
Scanner sn= new Scanner(fis);

while ((strLine = br.readLine()) != null) {
        if (strLine.startsWith("H") && ln.getLineNumber() == 0){ 
          //first line checking, throw exception
        }
       if (strLine.startsWith("T") && !sn.hasNextLine())  {
           //last line checking,throw exception
       }
}

yes, you’ll have to modify the code and add the validation in. The sample I pasted is printing variables that you could use to do your validation.

i think you should only use one of the readers to traverse the file.

Here’s what you’ve done:

Hey, Brad (BufferedReader)
Yeah?
Read this file for me, okay?
Sure!
Hey, Sam (Scanner)
Yeah?
Scan this file for me, okay?
Sure!
Alright, Brad, read the next line until there aren’t anymore.
Okay
Sam?
Yeah?
While Brad is doing that, you keep looking at the first line and let me know if it ever magically becomes the last line, okay?
Got it.

Thanks rushiku.

This seem like what I’ve done in the while loop and 2nd if