473,320 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 3113
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: oafyuf | last post by:
Hi, I'm having performanbce issues with StreamReader and was wondering what I could do to improve it... The following takes around 3 seconds to process! The content of the response is: ...
5
by: Anders Olsson | last post by:
I need to create a StreamReader which starts from the position of an existing StreamReader. This way, the second StreamReader can read lines "ahead" in an own loop, and then I can go back to the...
2
by: Keith Kingsley | last post by:
I'm using a StreamReader to read in several lines from an ASCII file. I'd like to know the StreamReader's "true" position-- that is, the number of bytes into the file that the StreamReader has...
3
by: Arno | last post by:
Hi, I'm using TcpClient for communication between two PC running a small piece of software. The protocol used has been designed internally and is HTTP similar (command line, headers, body). A...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
3
by: Arpan | last post by:
A file can be read using only the StreamReader object like this: Dim sReader As StreamReader sReader = New StreamReader(Server.MapPath("File1.txt")) While(sReader.Peek -1)...
1
by: garyusenet | last post by:
>From MSDN I'm trying to learn more about streamreader. I'm working my way down the MSDN definition. The first entry is the Public Constructor StreamReader. I'm fairly happy I have a basic grasp on...
1
by: Sladan | last post by:
Im trying to read a xml-file with a StreamReader. For the moment I'm using the following code. streamReader = new StreamReader(stream, System.Text.Encoding.Default); string feedData =...
0
by: rajana | last post by:
Dear All, We have Ansi file with german characters (Ä / Ø) , We are using Streamreader to read the contents of the file. But Readline() not able to read the German characters. We tried all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.