Reading ascii txt file into array and writing it.
I'm reading a txt file into an array and then writing a new file one line at a time. The reason i'm using array is that i'm searching for a few lines that i want to find and replace.
However in my file I have some ascii characters but they aren't read well. I read something about System.Text.Encoding.Default but I don't know how to implement it in my code. Can anyone help me with this?
Here's my code
Code:
Dim line As New List(Of String)
line.AddRange(IO.File.OpenText("C:\hello.txt").ReadToEnd.Split(Environment.NewLine))
'System.Text.Encoding.Default
Using sw As StreamWriter = File.CreateText("C:\goodbye.txt")
Dim linel As String
For Each linel In line
sw.WriteLine(linel)
Next
sw.Flush()
End Using
Re: Reading ascii txt file into array and writing it.
vb Code:
Dim in As New IO.FileStream("C:\hello.txt", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
Dim tr As New IO.StreamReader(in, Text.Encoding.GetEncoding("input encoding name"))
Dim out As New IO.FileStream("C:\goodbye.txt", IO.FileMode.OpenOrCreate, IO.FileAccess.Write, IO.FileShare.Read)
Dim tw As New IO.StreamWriter(out, Text.Encoding.GetEncoding("output encoding name"))
Dim Str As String
While Not tr.EndOfStream
Str = tr.ReadLine()
' Do somethigng wtih the text line
tw.WriteLine(Str)
End While
tw.Flush
Re: Reading ascii txt file into array and writing it.
Thanks here is the working code.
vb Code:
Dim fo As New IO.FileStream("C:\hello.txt", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
Dim tr As New IO.StreamReader(fo, System.Text.Encoding.GetEncoding("iso-8859-1"))
Dim out As New IO.FileStream("C:\goodbye.txt", IO.FileMode.OpenOrCreate, IO.FileAccess.Write, IO.FileShare.Read)
Dim tw As New IO.StreamWriter(out, System.Text.Encoding.GetEncoding("iso-8859-1"))
Dim Str As String
While Not tr.EndOfStream
Str = tr.ReadLine()
' Do somethigng wtih the text line
tw.WriteLine(Str)
End While
tw.Flush