SIGN UP MEMBER LOGIN:    
ARTICLE

Read and write the text file using vb.net

Posted by Sunil Atkari Articles | C# Language July 31, 2009
The code snippet in this article shows how to read and write text files in VB.NET.
Reader Level:

Here I have created a console application. It will help you to read and write a text file using file handling concepts of VB.NET.

If you want to use file handling concept of vb.net then you have to import System.IO Namespace. Basically Namespace is logical group of types. In this case we are importing IO related features.

In below code, first I have created one new text file. After that I have read another text file then doing some process means writing 80 characters in each and every line of another text file, which was created. The record length of each and every line will be 80 characters.

Public
Sub FileHandler() 
   Try 
      Dim strf As String = "WriteFile.log" 
      Dim fws As New FileStream(strf, FileMode.Create, FileAccess.Write) 
      Dim write As StreamWriter 
      write =
New StreamWriter(fws) 
      Dim strfile As String = "ReadFile.log" 
      Dim text As String = Nothing 
      Dim fs As New FileStream(strfile, FileMode.Open, FileAccess.ReadWrite) 
      Dim read As StreamReader 
      read =
New StreamReader(fs) 
      read.BaseStream.Seek(0, SeekOrigin.Begin) 
      Dim rd As String = Nothing 
      While (read.Peek() > -1) 
         text = Trim(read.ReadToEnd()) 
      End While 
      _textCount = Len(text) 
      Dim _count As Integer = 0 
      Dim i As Integer = 0 
      For i = 1 To text.Length 
         rd = Mid(text, i, 80) 
         _count = _count + 80 
          i = i + 79 
         write.BaseStream.Seek(0, SeekOrigin.End) 
         If _count = _textCount Or _count > _textCount Then 
            write.Write(rd) 
         Else 
            write.Write(rd + vbCrLf) 
          End If 
       Next 
        read.Close() 
         write.Close()
Catch ex As Exception 
      Throw
End Try
End Sub

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor