to write string in a text file

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
I can write a line of string in a text file but the text always write at the first line of the file which means I could not achieve to write a string at the under line of first or end of the file.
imageszz.png

Private Sub MainForm_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles MyBase.FormClosed
            'length of ful date digits
            Dim lenOfDate As Integer = 11
            'mytxtFile is my text file to sum the previous number of data in it
            ' that how many kb/s data used when close the program
            Dim readNumber As New FileStream(mytxtFile, FileMode.Open)
            Dim br As New StreamReader(readNumber)
            'to read number of data from the file to determine with date
            Dim lineInFile As String = br.ReadLine
            br.Close()
            'to get numbers of data in the line after skiping date
            Dim countOfUsage As String = lineInFile.Substring(lenOfDate, ((lineInFile.Length) - (lenOfDate)))
            'to convert string to integer 
            Dim numbersInFile As Integer = CType(countOfUsage, Integer)

            'to get read the file for operations
            Dim FS As New FileStream(mytxtFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)
            Dim SW As New IO.StreamWriter(FS)
            'to write date and some transmission's data count
            SW.WriteLine((Now.Date.ToString("yyyy-MM-dd") & "#") & (numbersInFile + collectRecieveCompute + _
                                                                    collectSentCompute + collectTotall).ToString("###"))
            SW.Close()
        End Sub
 
To append you can use the File class, or StreamWriter.
File Methods (System.IO)
StreamWriter Constructor (System.IO)
I know the Append function but I could not used it when I tried to add it in my project and I have not stayed enough after my fail to use it. Thanks I got it after your message that you trigger me off :)
This is completely nice I found now and explain clearly
    Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Dim sw As StreamWriter

        ' This text is added only once to the file. 
        If File.Exists(path) = False Then
            ' Create a file to write to.
            sw = File.CreateText(path)

            sw.WriteLine("Hello")
            sw.WriteLine("And")
            sw.WriteLine("Welcome")
            sw.Flush()
            sw.Close()
        End If

        ' This text is always added, making the file longer over time 
        ' if it is not deleted.
        sw = File.AppendText(path)
        sw.WriteLine("This")
        sw.WriteLine("is Extra")
        sw.WriteLine("Text")
        sw.Flush()
        sw.Close()

        ' Open the file to read from. 
        Dim sr As StreamReader = File.OpenText(path)
        Dim s As String
        Do While sr.Peek() >= 0
            s = sr.ReadLine()
            Console.WriteLine(s)
        Loop
        sr.Close()
    End Sub
 
Hi,

There is nothing wrong with the code that you have found but here is another example of reading, writing and appending to files with the StreamReader and StreamWriter. Maybe you can take some pointers from this:-

VB.NET:
Dim MyFile As String = "d:\temp\MyFileText.txt"
 
'using this constructor of the StreamWriter class defining the "Append" parameter as False
'causes a new file to be created if the file does not exist otherwise it will overwrite 
'an existing file if the file does exist
Using mySW As New StreamWriter(MyFile, False)
  With mySW
    .WriteLine("Hello")
    .WriteLine("And")
    .WriteLine("Welcome")
    .Close()
  End With
End Using
 
'using this constructor of the StreamWriter class defining the "Append" parameter as True
'causes a new file to be created if the file does not exist otherwise the existing file is
'appended to
Using mySW As New StreamWriter(MyFile, True)
  With mySW
    .WriteLine("This")
    .WriteLine("is Extra")
    .WriteLine("Text")
    .Close()
  End With
End Using
 
'There is no need to use Peek since here the file is read until the EndOfStream is reached
Using mySR As New StreamReader(MyFile)
  Dim myLine As String
  Do While Not mySR.EndOfStream
    myLine = mySR.ReadLine()
    Console.WriteLine(myLine)
  Loop
  mySR.Close()
End Using
 
'Notice that in all cases the file access routines are encased in Using blocks
'to ensure that all objects are disposed when finished with and all resources are
'released back to the system

Cheers,

Ian
 
Back
Top