471,570 Members | 892 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

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(object 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(textBox1.Text))
{
Int32 count=0;
SW = new StreamWriter("TestFile.txt");
while ((line = SR.ReadLine()) != null)
{
SSNO=line.Substring(SSNO_F, SSNO_L);
count += 1;
sw.WriteLine("SSNO is : {0}", SSNO);
}
}
////omited

}
Jan 29 '06 #1
4 3022
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
http://www.markdawson.org

"KenLee" wrote:
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(object 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(textBox1.Text))
{
Int32 count=0;
SW = new StreamWriter("TestFile.txt");
while ((line = SR.ReadLine()) != null)
{
SSNO=line.Substring(SSNO_F, SSNO_L);
count += 1;
sw.WriteLine("SSNO is : {0}", SSNO);
}
}
////omited

}

Jan 29 '06 #2
Mark R. Dawson <Ma*********@discussions.microsoft.com> wrote:
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.


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 29 '06 #3
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:
Mark R. Dawson <Ma*********@discussions.microsoft.com> wrote:
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.


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

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

Jan 29 '06 #4
Mark R. Dawson <Ma*********@discussions.microsoft.com> wrote:
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?


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by oafyuf | last post: by
2 posts views Thread by Keith Kingsley | last post: by
21 posts views Thread by JoKur | last post: by
3 posts views Thread by Arpan | last post: by
1 post views Thread by Sladan | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.