开发者

how to read and write into a file using streamwriter and streamreader

I am trying to read the content from a .csv file and storing it in a variable. Later I am writing that content into a newfile. Code is executing successfully but the data is not appearing in new file. Any suggestions please?

Here is my code:

开发者_Python百科
    Dim ioFile As New System.IO.StreamReader("C:\sample.csv")

    Dim ioLine As String 
    Dim ioLines As String 
    ioLine = ioFile.ReadLine
    ioLines = ioLine
    While Not ioLine = ""
        ioLine = ioFile.ReadLine
        ioLines = ioLines & vbCrLf & ioLine

    End While
    Dim ioWriter As New System.IO.StreamWriter("C:\new.csv")
    ioWriter.WriteLine(ioLines)
    ioFile.Close()
    ioWriter.Close()


There are some very short and very good tutorials on the MSDN site:

  1. How to: Write text to a file
  2. How to: Read text from a file


I was looking for an answer , cos I had the same problem like U.
Below I share you my source code. It works, try it.

    If Not System.IO.File.Exists(path + "\" + fileName) Then
        System.IO.File.Create(path + "\" + fileName).Dispose()
    End If

    Dim sr As New StreamReader(path + "\" + fileName)
    Dim NumberOfLines As Integer

    Do While sr.Peek >= 0
        sr.ReadLine()
        NumberOfLines += 1
    Loop
    NumberOfLines = NumberOfLines + 1
    sr.Close()
    sr.Dispose()

    Dim sw As New System.IO.StreamWriter(file_path, False)
    sw.WriteLine(NumberOfLines.ToString().PadLeft(2, "0") + vbTab + "Your content")
    sw.Close()
    sw.Dispose()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜