Results 1 to 3 of 3

Thread: Reading ascii txt file into array and writing it.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    The Netherlands
    Posts
    94

    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
    "Against All Odds"

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Reading ascii txt file into array and writing it.

    vb Code:
    1. Dim in As New IO.FileStream("C:\hello.txt", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
    2. Dim tr As New IO.StreamReader(in, Text.Encoding.GetEncoding("input encoding name"))
    3. Dim out As New IO.FileStream("C:\goodbye.txt", IO.FileMode.OpenOrCreate, IO.FileAccess.Write, IO.FileShare.Read)
    4. Dim tw As New IO.StreamWriter(out, Text.Encoding.GetEncoding("output encoding name"))
    5.  
    6. Dim Str As String
    7. While Not tr.EndOfStream
    8.     Str = tr.ReadLine()
    9.     ' Do somethigng wtih the text line
    10.     tw.WriteLine(Str)
    11. End While
    12. tw.Flush

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Location
    The Netherlands
    Posts
    94

    Re: Reading ascii txt file into array and writing it.

    Thanks here is the working code.
    vb Code:
    1. Dim fo As New IO.FileStream("C:\hello.txt", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
    2. Dim tr As New IO.StreamReader(fo, System.Text.Encoding.GetEncoding("iso-8859-1"))
    3. Dim out As New IO.FileStream("C:\goodbye.txt", IO.FileMode.OpenOrCreate, IO.FileAccess.Write, IO.FileShare.Read)
    4. Dim tw As New IO.StreamWriter(out, System.Text.Encoding.GetEncoding("iso-8859-1"))
    5.  
    6. Dim Str As String
    7. While Not tr.EndOfStream
    8.     Str = tr.ReadLine()
    9.     ' Do somethigng wtih the text line
    10.     tw.WriteLine(Str)
    11. End While
    12. tw.Flush
    "Against All Odds"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width