Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to a Stream

Status
Not open for further replies.

uzzie123

Technical User
Mar 7, 2007
21
0
0
CA
Hi,

I am trying to write some text to this text document. Even the the program successfully debugs, i do not see a blank document on the file that I want to make changes on.

Here is the code:

using System;

using System.IO;

class CheckStream
{
public static void Main()
{



string fName = "report info.txt";

StreamWriter inStream;

try{

inStream = new StreamWriter(fName);
inStream.WriteLine("1234567");
}
catch(Exception e)
{
Console.WriteLine("What!! {0}", e);
}

}
}
 
Code:
inStream = new StreamWriter(fName);
inStream.WriteLine("1234567");
[COLOR=green]// this will flush the contents to the stream then close the file handle.  Or, call inStream.Flush() first.[/color]
inStream.Close()
Hope this helps [wink]
 
A minor point, but why it your output stream called "inStream" - much better to call it "outStream" or something with "out" rather than "in" - because you are writing data out - not reading it in.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
try{
inStream = new StreamWriter(fName);
inStream.WriteLine("1234567");

//Add this line
inStream.Flush(); //write the buffer to the file
}
catch(Exception e)
{
Console.WriteLine(ex.Message + System.Environment.NewLine + ex.StackTrace);
}
finally
{
inStream.Close();
}
 
flush, close and finally all in one:

Code:
using ( StreamWriter inStream = new StreamWriter(fname) )
{
  inStream.WriteLine("1234567");
}



mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top