VB.NET

Moderators: seancampbell
Number of threads: 4020
Number of posts: 10026

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Read from and write to a text file in vb.net Posted by soweyoung on 23 Jul 2004 at 8:07 AM
This message was edited by soweyoung at 2004-7-23 8:10:53

Hello everyone,
I am working on a small program in vb.net which reads a line at a time from a text file and writes a new line back to the file.

What I am trying to do is that I read from a text file line by line and want to be able to write a new line at the every third line.
The code is below. Any comments would be appreciated.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim inputString As String
Dim sb As New StreamReader("C:\test.txt")
Dim i As Long

i = 1
inputString = sb.ReadLine

While Not inputString Is Nothing
If i = 3 Then
'want to write a line of message to a file right at this position
i = 0
End If

i += 1
inputString = sb.ReadLine
End While
End Sub


Report
Re: Read from and write to a text file in vb.net Posted by kainsworth on 25 Jul 2004 at 2:05 PM
This message was edited by kainsworth at 2004-7-25 14:8:18

There are many ways of doing this. Here is just one approach - I prefer to read from file into an array or collection, make any amendments and then rewrite the file to reflect the amendments.
One version of code that will achieve this is as follows:
'  At top of Form code
Imports System.IO
Imports System.Collections.Specialized

'  The following code to be fired from an appropriate event:
        Dim MyFileText As New StringCollection  ' A collection
        Dim Fstrm As FileStream = New FileStream("MyTextFile.txt", FileMode.Open)
        '  Instantiate the StreamReader and pass it the FileStream 
        Dim StrmR As StreamReader = New StreamReader(Fstrm)
        '  Read the whole file into the collection
        While StrmR.Peek <> -1
            MyFileText.Add(StrmR.ReadLine())
        End While
        '   close the file.  
        Fstrm.Close()

        '  Edit the collection (every 3rd line)
        Dim i As Integer
        For i = 2 To MyFileText.Count - 1 Step 3
            MyFileText.Item(i) = "Replaced in Code"
        Next
        
        '  Kill the original file, then recreate it
        File.Delete("MyTextFile.txt")
        Dim FS As FileStream = New FileStream("MyTextFile.txt", FileMode.CreateNew)
        '  Write amended data block back to recreated file
        Dim sw As StreamWriter = New StreamWriter(FS)
        For i = 0 To MyFileText.Count - 1
            sw.WriteLine(MyFileText.Item(i).ToString)
        Next
        sw.Close()
        Fstrm.Close()

The above will not be everybody's preferred route, and some may think the StringCollection to be overkill, but it works for me. If you add a decent level of exception handling to the core code, it is a fairly robust way of going about it.


Report
Re: Read from and write to a text file in vb.net Posted by softwareskill on 2 Feb 2013 at 3:54 AM
Here's a simple explanation on how to read data from and write data to text file in asp.net http://www.freedotnetapps.com/asp-net/how-to-read-data-from-and-write-data-to-text-file-in-asp-net/



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.