Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

vb.net

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:

    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()
like image 829
Ram Avatar asked Jun 02 '11 12:06

Ram


People also ask

What is StreamReader and StreamWriter?

The StreamReader and StreamWriter classes are used for reading from and writing data to text files. These classes inherit from the abstract base class Stream, which supports reading and writing bytes into a file stream.

What is the difference between a StreamReader and StreamWriter objects?

A StreamReader is used whenever data is required to be read from a file. A Streamwriter is used whenever data needs to be written to a file.


1 Answers

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
like image 74
Uwe Keim Avatar answered Oct 11 '22 16:10

Uwe Keim