Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

StreamWriters and StreamReaders

streamwriter stream

  • Please log in to reply
5 replies to this topic

#1 Cushpajz

Cushpajz

    CC Regular

  • Member
  • PipPipPip
  • 32 posts
  • Location:Novi Sad
  • Programming Language:C#
  • Learning:C, C++, C#, PHP

Posted 17 December 2011 - 03:46 AM

I saw that there is no tutorials about streamwriters and readers so I will now try to make one.
First of all StreamWriters and StreamReaders are used for writing or reading from some .txt file, later you can do whatever you want from them, put them into loops, or whatever. So let's start.
First thing you will do is to call IO(input/output) library.
using System.IO;
Than you can put start using streamwriter or reader in events. In this case I will be using an openFileDialog and saveFileDialog to show you how you can select file you want to read or give the file a path where you want to save it:
private void button1_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
                sw.Write(richTextBox1.Text);
                sw.Close();
            }
        }
Now I will explain every line
First of all you see that if you want to save something to a .txt file you must use savefiledialog, and streamWriter (it's not necessary to use save file dialog, you can just put a path whre you want to save it)
So first of all you create new streamwriter by telling him where to save that file( in this case this is the path you select from save file dialog).
Second you use Write() method, to write all text from textbox, you can also use WriteLine() method for writing just a single line.
If you call this:
sw.WriteLine(richtextbox1.Text);
sw.WriteLine(richtextbox1.Text);
you will get just first 2 lines from that text box.
Okay, so you finished writing to a .txt file, now what you are going to do is to close that writer by calling a method Close();
You need to close reader or writer every time when you finish because if you dont, he will just continue and you will get an error.
In this example I will show just some examples of path usage in streamwriter(the same is used for readers).
StreamWriter sw = new StreamWriter(@"C:\\Documents\YourFolder", "filename.txt"); //example of giving exact path where you want to save something, you can put whatever path you want I'm just giving an example.
StreamWriter sw = new StreamWriter("filename.txt"); //It puts your file to the Debug Folder, right next to the .exe file
StreamWriter sw = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop, "filename.txt"); // It puts file to the desktop, on every computer.
Okay now that you know what StreamWriter is, let's go to StreamReader, it is pretty much the same.
In this example I will show how to read to textbox from txt file, calling a streamreader from openFileDialog.
private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(openFileDialog1.FileName);
                richTextBox1.Text = sr.ReadToEnd();
                sr.Close();
            }
        }
So basically it is the same, First line of StreamReader is the same as StreamWriter, just for reader you put path where you want it to READ from.
Now i will explain just 2 of the most used methods.
ReadToEnd() - reads all text from .txt file and puts it into a textbox or label or whatever you put.
ReadLine() - reads just one line.
You can also set streamreader an exact path: @"location". So everything I told for StreamWriter stays for StreamReader.

I hope I helped, and that you now understand writer and reader.
Tutorial by Cushpajz.

Ps. If you are putting this in other forums, please put an author. Thanks.
  • 0

#2 Sublime

Sublime

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 13 January 2012 - 03:18 PM

Thanks Cush ;)
  • 0

#3 Rishi Sharma

Rishi Sharma

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 22 February 2012 - 11:42 AM

Thanks for the Tutorial -Simple and easy to grab,

But I have a question (I know this might seem noobish , but I dont want to keep this confusion/doubt)-

While using StreamReader class , do we have a method called Read(); or just ReadLine(); and ReadToEnd();
What I mean is that ,whether this line is valid or not ?-"
StreamReader sread=new StreamReader(filepath);
RichTextBox1.Text=sread.Read(); "

  • 0

#4 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts
  • Programming Language:C, C++, C#, Perl, Visual Basic .NET, Assembly, VBScript, Others

Posted 11 April 2012 - 05:43 PM

To dispose of the object though, you could/should be calling the dispose() method or utilizing the "using" keyword for StreamReader/StreamWriter.

using (StreamReader sr = new StreamReader("file.txt"))
{

}

@Rishi Sharma - There is a method called Read() as well in the StreamReader class.
  • 0

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image


#5 Bakr

Bakr

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 13 April 2012 - 09:03 AM

thx alot i wanted to ask what deff between streamreader and textreader?... thx


  • 0

#6 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts
  • Programming Language:C, C++, C#, Perl, Visual Basic .NET, Assembly, VBScript, Others

Posted 13 April 2012 - 02:50 PM

thx alot i wanted to ask what deff between streamreader and textreader?... thx


StreamReader, reads a stream, which can also read a filestream, which is why you can read textfiles with it, TextReader only reads a series of characters, and you cannot read a stream with it.
  • 0

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download