StreamReader vs StreamWrite

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KenLee

    StreamReader vs StreamWrite

    help!!
    I used StreamReader and StreamWrite.
    the problem is it doesn't write all readline.
    For example it read 100 line and write 51lines.
    this is codes.

    class kenlee{
    private StreamWriter sw ;
    private StreamReader sr;
    private String line;
    private String SSNO;
    private const int SSNO_F = 13, SSNO_L = 21;
    private void btnload_Click(o bject sender, EventArgs e)
    {
    read();

    }

    void read()
    {

    try
    {
    // Create an instance of StreamReader to read from a file.
    // The using statement also closes the StreamReader.
    using (StreamReader SR = new StreamReader(te xtBox1.Text))
    {
    Int32 count=0;
    SW = new StreamWriter("T estFile.txt");
    while ((line = SR.ReadLine()) != null)
    {
    SSNO=line.Subst ring(SSNO_F, SSNO_L);
    count += 1;
    sw.WriteLine("S SNO is : {0}", SSNO);
    }


    }
    ////omited

    }
  • Mark R. Dawson

    #2
    RE: StreamReader vs StreamWrite

    Hi KenLee,
    you want to make sure you close your streams properly, then you will see
    all the data being written to the file, otherwise it may not have flushed all
    data from its underlying buffer into the file. Make sure you explicitly call
    the Close method on the StreamWriter inside a finally statement and you will
    be in good shape.


    Hope that helps
    Mark Dawson




    "KenLee" wrote:
    [color=blue]
    > help!!
    > I used StreamReader and StreamWrite.
    > the problem is it doesn't write all readline.
    > For example it read 100 line and write 51lines.
    > this is codes.
    >
    > class kenlee{
    > private StreamWriter sw ;
    > private StreamReader sr;
    > private String line;
    > private String SSNO;
    > private const int SSNO_F = 13, SSNO_L = 21;
    > private void btnload_Click(o bject sender, EventArgs e)
    > {
    > read();
    >
    > }
    >
    > void read()
    > {
    >
    > try
    > {
    > // Create an instance of StreamReader to read from a file.
    > // The using statement also closes the StreamReader.
    > using (StreamReader SR = new StreamReader(te xtBox1.Text))
    > {
    > Int32 count=0;
    > SW = new StreamWriter("T estFile.txt");
    > while ((line = SR.ReadLine()) != null)
    > {
    > SSNO=line.Subst ring(SSNO_F, SSNO_L);
    > count += 1;
    > sw.WriteLine("S SNO is : {0}", SSNO);
    > }
    >
    >
    > }
    > ////omited
    >
    > }[/color]

    Comment

    • Jon Skeet [C# MVP]

      #3
      RE: StreamReader vs StreamWrite

      Mark R. Dawson <MarkRDawson@di scussions.micro soft.com> wrote:[color=blue]
      > you want to make sure you close your streams properly, then you will see
      > all the data being written to the file, otherwise it may not have flushed all
      > data from its underlying buffer into the file. Make sure you explicitly call
      > the Close method on the StreamWriter inside a finally statement and you will
      > be in good shape.[/color]

      Or (preferrably IMO) use "using" a statement around the StreamWriter.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Mark R. Dawson

        #4
        RE: StreamReader vs StreamWrite

        Hi John,
        I would most definitely recommend that too, but is the IDisposable
        interface implemented explicitly on the StreamWriter class I do not see it as
        a public method?

        Mark.

        "Jon Skeet [C# MVP]" wrote:
        [color=blue]
        > Mark R. Dawson <MarkRDawson@di scussions.micro soft.com> wrote:[color=green]
        > > you want to make sure you close your streams properly, then you will see
        > > all the data being written to the file, otherwise it may not have flushed all
        > > data from its underlying buffer into the file. Make sure you explicitly call
        > > the Close method on the StreamWriter inside a finally statement and you will
        > > be in good shape.[/color]
        >
        > Or (preferrably IMO) use "using" a statement around the StreamWriter.
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        > If replying to the group, please do not mail me too
        >[/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          RE: StreamReader vs StreamWrite

          Mark R. Dawson <MarkRDawson@di scussions.micro soft.com> wrote:[color=blue]
          > I would most definitely recommend that too, but is the IDisposable
          > interface implemented explicitly on the StreamWriter class I do not see it as
          > a public method?[/color]

          Yes, it's done with explicit implementation - TextWriter itself has
          IDisposable.Dis pose.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          Working...