I am a new user and struggling with the below:
I would need to read the file and write back with some modifications, the issue is:
- “ReadToEnd” works fine for smaller files [100MB approx] and everything works fine what I like to do. But for Bigger Size Files [300 MB +], it bombs out.
- Then I tried “ReadLine” (Reading Line By Line) It works fine on the smaller or bigger files but it takes very very long to save back.
I have included both of the codes below "ReadToEnd" and "ReadLine" For testing you would need to create "100MB-File.txt" and "300MB-File.txt" files in c:\temp\ area.
I would really appreciate your help in this regard
'----------------Reading whole File ReadToEnd
Dim sr As New StreamReader("C:\temp\100MB-File.txt")
Dim path As String = "C:\temp\myFileNew1.txt"
Dim oneLine As String
oneLine = sr.ReadToEnd
Using sw As StreamWriter = File.CreateText(path)
sw.WriteLine(oneLine)
End Using
sr.Close()
''---------------Reading Line by Line
Dim sr As New StreamReader("C:\temp\300MB-File.txt")
Dim path As String = "C:\temp\myFileNew2.txt"
Dim oneLine As String
oneLine = sr.ReadLine
Using sw As StreamWriter = File.CreateText(path)
sw.WriteLine(oneLine)
End Using
Do Until sr.EndOfStream
Console.WriteLine(oneLine)
oneLine = sr.ReadLine()
Using sw As StreamWriter = File.AppendText(path)
sw.WriteLine(oneLine)
End Using
Loop
sr.Close()