is there any other alternative in which lines can be deleted , as i am having only 4 GB of RAM , kindly suggest
with regards
Hi,
Besides IO.File and IO.File namespaces, we can also use IO.StreamReader and IO.StreamWriter namespaces to read and write files and they operate text files line by line.
However, for the reason it will not read a text file into an array/list, we cannot use List.RemoveRange method to delete lines. To handle this issue, I set a new List called delLineIndex to store the index of those lines which need to be delete. When we
write lines to the new file, we ignore these marked lines.
Here is the code:
Dim delLineIndex As New List(Of Integer)
delLineIndex.Add(6)
delLineIndex.Add(7)
delLineIndex.Add(8)
Dim openStream = New StreamReader("O:\ReportAll.txt")
Dim saveStream = New StreamWriter("O:\ReportAll_new.txt")
Dim lineStr As String = ""
Dim lineIndex As Integer = 0
Do
lineStr = openStream.ReadLine()
If lineStr Is Nothing Then
Exit Do
Else
lineIndex += 1
If Not delLineIndex.Contains(lineIndex) Then
saveStream.WriteLine(lineStr)
End If
End If
Loop Until lineStr Is Nothing
openStream.Close()
saveStream.Close()
You can find it will delete line 6, 7 and 8 from the original text file. Hope it helps you.
For more information on IO.StreamReader and IO.StreamWriter namespaces, you can refer to MSDN:
Shengqing Ya...
All-Star
45968 Points
2997 Posts
Re: how to delete few lines from the text file either with line number or with content
Apr 08, 2009 08:42 AM|LINK
Hi,
Besides IO.File and IO.File namespaces, we can also use IO.StreamReader and IO.StreamWriter namespaces to read and write files and they operate text files line by line.
However, for the reason it will not read a text file into an array/list, we cannot use List.RemoveRange method to delete lines. To handle this issue, I set a new List called delLineIndex to store the index of those lines which need to be delete. When we write lines to the new file, we ignore these marked lines.
Here is the code:
You can find it will delete line 6, 7 and 8 from the original text file. Hope it helps you.
For more information on IO.StreamReader and IO.StreamWriter namespaces, you can refer to MSDN:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx
Best Regards,
Shengqing Yang
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework