StreamReader vs StreamWrite

G

Guest

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

}
 
G

Guest

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
 
J

Jon Skeet [C# MVP]

Mark R. Dawson said:
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.
 
G

Guest

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.
 
J

Jon Skeet [C# MVP]

Mark R. Dawson said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top