user1804925
user1804925

Reputation: 159

Check whether if the text file has the extra blank line or not (system out of memory)

I have a delimited file and need to remove the last line if it appears twice.

enter image description here

    fileLines = IO.File.ReadAllLines(fileDirectory)
    extraBlankLine = fileLines(fileLines.Length - 1)

If it return "nothing" then I know there is a extra blank line, then it will remove the last line from the file.

Sometimes I will get the "System out of Memory" error due to the file size (up to 2GB).

Is there other ways to check whether the file has the double blank lines at the end or not?

Upvotes: 0

Views: 1115

Answers (1)

yk11
yk11

Reputation: 768

Read each line individually, keeping track of the current and previous lines. When you get to the end of the file, check their values.

Dim reader = File.OpenText(fileDirectory)
Dim line as String = ""
Din line2 as String = ""
Dim line3 as String = ""

Do
    line3 = line2
    line2 = line
    line = reader.ReadLine
Loop Until line Is Nothing

reader.Close()

If String.IsNullOrEmpty(line2) And String.IsNullOrEmpty(line3) Then
    ' Do what you need to do
End If

Upvotes: 1

Related Questions