locked
whats the difference between Filestream and streamwriter RRS feed

  • Question

  • I see i can use a filestream or stream writer whats the difference?

     Private Sub FileIO()
            ' Write to a file
            Dim sw As New StreamWriter("c:\test.txt")
            sw.WriteLine("This is a line in the file.")
            sw.WriteLine("This is another line in the file.")
            sw.Close()

            ' Read from a file
            Dim existingFile As New StreamReader("c:\test.txt")
            Dim nextLine As String

            Do
                nextLine = existingFile.ReadLine()
                Console.WriteLine(nextLine)
            Loop Until nextLine Is Nothing

            existingFile.Close()
        End Sub

        Private Sub FileInfoClass()
            ' FileInfo class
            Dim fi As New FileInfo("c:\test.txt")

            If Not fi.Exists Then
                Dim fiStream As FileStream = fi.Create
                fiStream.Close()
                fi = New FileInfo("c:\test.txt")
            End If

            Dim sw As StreamWriter = fi.CreateText

            sw.WriteLine("This is a line in the file.")
            sw.Flush()
            sw.Close()

            sw = fi.AppendText
            sw.WriteLine("This is another line in the file.")
            sw.Flush()
            sw.Close()

            Console.WriteLine("File name: {0}", fi.Name)
            Console.WriteLine("Full name: {0}", fi.FullName)
            Console.WriteLine("Size: {0}", fi.Length)

            fi.CopyTo("c:\test2.txt")
            fi.Delete()
        End Sub

        Private Sub DirectoryInfoClass()
            Dim dir As New DirectoryInfo("c:\demos")
            Dim fs As FileStream
            If Not dir.Exists Then
                dir.Create()

                Console.WriteLine("Has c:\demos been created?")
                Console.ReadKey()
            End If
            'Was deleted last line of prior sub
            Dim fi As New FileInfo("c:\test.txt")
            If Not fi.Exists Then
                fs = fi.Create
                fs.Close()
                fi = New FileInfo("C:\test.txt")
            End If
            fi.CopyTo(dir.FullName & "\test.txt")
            Console.WriteLine("Has text.txt been copied to c:\demos?")
            Console.ReadKey()

            dir.Delete(True)
            Console.WriteLine("Has c:\demos been deleted?")
        End Sub
    McC
    Tuesday, May 12, 2009 2:27 PM

Answers

  • A StreamWriter can write to all kinds of different streams (e.g. FileStreams, MemoryStreams, etc.).  It inherits from TextWriter and deals with writing to these streams. 

    StreamWriter Summary:

    Implements a System.IO.TextWriter for writing characters to a stream in a particular encoding.



    A FileStream inherits from System.IO.Stream and can be used to read/write from files specifically.

    FileStream Summary:

    Exposes a System.IO.Stream around a file, supporting both synchronous and asynchronous read and write operations.

     

    • Edited by bpell Tuesday, May 12, 2009 4:20 PM
    • Proposed as answer by A.m.a.L Hashim Tuesday, May 12, 2009 6:15 PM
    • Marked as answer by Yichun Feng Thursday, May 14, 2009 6:29 AM
    Tuesday, May 12, 2009 4:18 PM

All replies

  • To me, they are the same. if you use StreamWriter and StreamReader without the FileStream, FileStream will automatically created. But when you created FileStream by yourself, you have options to specify file mode and share access
    Dim fs As New FileStream("C:\test.txt", FileMode.Create, FileAccess.Read, FileShare.None)

    kaymaf

    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Tuesday, May 12, 2009 4:17 PM
  • A StreamWriter can write to all kinds of different streams (e.g. FileStreams, MemoryStreams, etc.).  It inherits from TextWriter and deals with writing to these streams. 

    StreamWriter Summary:

    Implements a System.IO.TextWriter for writing characters to a stream in a particular encoding.



    A FileStream inherits from System.IO.Stream and can be used to read/write from files specifically.

    FileStream Summary:

    Exposes a System.IO.Stream around a file, supporting both synchronous and asynchronous read and write operations.

     

    • Edited by bpell Tuesday, May 12, 2009 4:20 PM
    • Proposed as answer by A.m.a.L Hashim Tuesday, May 12, 2009 6:15 PM
    • Marked as answer by Yichun Feng Thursday, May 14, 2009 6:29 AM
    Tuesday, May 12, 2009 4:18 PM
  • FileStream represents the binary representation of a file.  If you look the methods, you'll see that it works with bytes and byte arrays.  Which is great when you are working with binary data - such as image files.  But, many times you're working with text files - StreamReader/StreamWritter work at this level, with methods like ReadLine, WriteLine, etc.

    When you create a StreamReader/StreamWriter from a path, then it creates a FileStream internally and uses that to do the actuall reading.

    HTH
    Tom Shelton
    Tuesday, May 12, 2009 4:26 PM
  • thanks!
    McC
    Tuesday, May 12, 2009 6:03 PM