Breaking News

Thursday 26 September 2013

c# File IO using Reading from and Writing into Binary Files

The BinaryReader and BinaryWriter classes are used for reading from and writing to s binary file.

The BinaryReader class:

The BinaryReader classes is used to read Binary data from a file. A BinaryReader object is created by passing a FileStream Object to its constructor.

some of the commonly used method of BinaryReader class :


  • Close()  >> it closes the BinaryReader object and the underlying stream.
  • Read()  >> read the character from the underlying stream.
  • ReadBoolean()  >> reads a boolean value from current stream and advances current the position of the stream.
  • ReadByte()  >> reads the next bytes from the current stream.
  • ReadChar() >> reads the next character from the current stream.
  • ReadDouble() >> reads an floating points value from the current stream.
  • ReadString() >> reads a string from a current stream.
The BinaryWriter Class:

The BinaryWriter class is used to Write data to a stream. A BinaryWriter object is created by passing a FileStream object to its constructor.

some of the commonly used method of BinaryWriter class :

  • Close()  >> its close the BinaryWriter class object and underlying stream.
  • Flush()   >> clear all buffer for the current writer.
  • Seek(int offset , SeekOrigin Origin)  >> sets the position within the current stream.
  •  Write(bool value)  >> write a one-byte boolean value to the current stream.
  • Write(byte value)  >> write an unsigned byte to the current stream.
  • Write(byte[] buffer)  >> writes a byte array to the underlying stream.
  • Write(string value)  >> reads a string value from the current stream.

Example:


using System.IO;

namespace binaryreader_and_binarywriter_classes.cs
{
    public class RWDATA
    {
        static void Main(string[] args)
        {
            int i = 10;
            double d = 1023.23;
            bool b = true;

            FileStream fs = new FileStream("C://USING IO/testdata",FileMode.Create);
            BinaryWriter dataout = new BinaryWriter(fs);
            Console.WriteLine("writing"+i);
            dataout.Write(i);
            Console.WriteLine("writing"+b);
            dataout.Write(b);
            Console.WriteLine("writing"+d);
            dataout.Write(1023.23);
            dataout.Close();
            Console.WriteLine("");
            // now read them back
            FileStream fs1 = new FileStream("C://USING IO/testdata", FileMode.Open);
            BinaryReader datain = new BinaryReader(fs1);
            i = datain.ReadInt32();
            Console.WriteLine("reading "+ i);
            b = datain.ReadBoolean();
            Console.WriteLine("reading "+ b);
            d = datain.ReadDouble();
            Console.WriteLine("reading "+1023.23);
            Console.ReadLine();
            datain.Close();
        }
    }
}


output :




Reference Link :




Read more ...

C# File I/O using Read and Write into Text File

C# READING AND WRITING TO TEXT FILES :

the StreamReader and StreamWriter Classes are used for reading from and writing data to text files. these classes inherit from the base class stream , which supports reading and writing bytes into a file stream.

The StreamReader Class :

the StreamReader class also inherit from the abstract base class TextReader that represents a reader for reading series of characters.

some of the commonly used method of the StreamReader Class :


  • Close()  >> it closes the StreamReader object and Stream , and release any system resource associated with the reader.
  • Peek()   >> return the next available character but does not consume it.
  • Read()  >> reads the next character from the input stream.
example :

the following example demonstrates reading a text file named test.txt. the file reads :

using system;
using system.IO;

namespace fileapplication
{
   class program
     {
        static void Main( String[] args )
          {
             try
              {
                 // create an instance of StreamReader to read from a file.
                // the using statement also closed the StreamReader.
                  using( StreamReader sr = new StreamReader ( "C://test.txt"))
                    {
                      string line;
                        // read and display lines from the files until
                        // the end of the file is reached.
                        While (( line = sr.ReadLine()) != null)
                           {
                              Console.WriteLine(line);
                           }
                      }
                  }
    catch ( Exception e )
      {
         // let the user know what went wrong.
         Console.WriteLine(" the file could not be read.."+ e.Message);
      }
Console.ReadKey();
       }
    }
 }

 guess what display when you compile and run the program..

example 2..


using System.IO;

namespace srreamreader_class.cs

{

    class Program

    {
        public void readData()
        {
            FileStream fs = new FileStream("C://myfile/myfile.txt", FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            // position the file pointer at the beginning of the file..
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            // read till the end of the file
            string str = sr.ReadLine();
            while (str != null)
            {
                Console.WriteLine("" + str);
                str = sr.ReadLine();
            }
            sr.Close();
            fs.Close();
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.readData();
        }
    }
}


output :



reference link :


The StreamWriter Class :

the StreamWriter class inherits from the abstract from the abstract classes TextWriter represent a writer , which can write a series of a character.

the mos commonly used method of StreamWriter Class are:


  • Close()   >> close the current StreamWriter class and Stream.
  • Flush()  >> clears all buffers for current writer.
  • Write(bool value) >> write the text value to the text string or stream. ( inherited from TextWriter.)
  • Write(char value) >> writes a character to the stream.
  • Write(decimal value) >> write the text representation of decimal value to the text string or stream.
  • Write(double value) >> write the text representation of 8-byte floating point  value to the text string or stream.
  • Write( int value) >> write the text representation of 4-byte integer value to the text string or stream.
  • Write( string value) >> writes a string to the stream.
  • WriteLine() >> write a line to the stream.

the following code demonstrates writing text data into a file using the StreamWriter Class:


using System.IO;
namespace streamwriter_class.cs
{
    class Program
    {
        public void writedata()
        {
            FileStream fs = new FileStream("C://myfile/myfile.txt", FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            // enter a string which is stored in the file myfile.txt
            Console.WriteLine("enter a string..");
            string str = Console.ReadLine();
            //write the string entered by the user in the file myfile.txt
            sw.Write(str);
            // clear all buffer of the current writer
            sw.Flush();
            sw.Close();
            fs.Close();
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
           
            Program p = new Program();
            p.writedata();
        }
    }

}

output :




reference link:




click here to watch an example of StreamWriter Class .
Read more ...

how to use File IO using StreamReader class C#

Read more ...
© 2013 Post by repter_x