C# notes part ->6


File

Implementing file IO concepts:

Stream: A stream is a sequence of bytes traveling from source to destination over a communication path.

The streams are
1.  Input stream
2.  Output stream
Input stream is used for read operation.
Output stream is used for write operation.

In order to perform operations like  file creation, file deletion, read, write operations to a file, we require System.IO namespace.
System.IO
|
|
FileReader
StreamReader
StreamWriter
BinaryWriter
DirectoryInfo
FileInfo

If data of a file is only text then we can use StreamReader  and StreamWriter classes to perform reading and writing.


StreamReader class
            |
Close()
Peck()
Read()
ReadLine()
Seek()

FILE STREAM:  To open an existing file or to  create a new file, we require an object of type FileStream
Syntax:
FileStream obj = new FileStream(“filename”,FileMode enumerator,FileAccess enumerator,FileShare enumerator);

Write a program to read data from the existing file

using System;
using System.IO;

namespace ReadFileEx
{
    public class Sample
    {
        public void ReadData()
        {
            FileStream fs =new FileStream("c:\\abc.txt",FileMode.Open,FileAccess.Read);
            StreamReader sr =new StreamReader(fs);
            sr.BaseStream.Seek(0,SeekOrigin.Begin);
            string str= sr.ReadLine();
            while(str!=null)
            {
                Console.WriteLine(str);
                str=sr.ReadLine();

            }
            sr.Close();
            fs.Close();

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Sample s =new Sample();
            s.ReadData();
            Console.ReadLine();
        }
    }
}

StreamWriter:

This class used to write a series of characters to a file.

StreamWriter
         |
Close()
Flush()
Write()
WriteLine()

Flush():  Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.
using System;
using System.IO;
namespace CreateFileEx
{
    public class Sample
    {
        public void WriteData()
        {
             FileStream fs = new FileStream("c:\\xyz.txt",FileMode.Create,FileAccess.Write);
      
            StreamWriter sw = new StreamWriter(fs);
            Console.WriteLine("enter data into a file");
           
            string str = Console.ReadLine();
       
            sw.WriteLine(str);

            sw.Flush();
            sw.Close();
            fs.Close();           
                      
        }
     }

  class Program
    {
        static void Main(string[] args)
        {

            Sample s = new Sample();
            s.WriteData();
            Console.ReadLine();
        }
    }
}

binary file
BinaryReader and BinaryWriter classes are used to read and write binary data into binary files.
BinaryReader class is used to read the binary data from a file. This class has Close() and Read() methods.
BinaryWriter class is used to write binary data to a stream . This class has methods like Close(), Seek(),  Write(), Flush().

using System;
using System.IO;
namespace createbinaryfile
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 115;
            double d = 36045;
            bool b = true;
            FileStream fs = new FileStream("d:\\binaryfile", FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(i);
            Console.WriteLine("Writing :i    "+i);
            bw.Write(d);
            Console.WriteLine("Writing :d   "+d);
            bw.Write(b);
            Console.WriteLine("Writing :b   "+b);
            bw.Flush();
            fs.Close();
            BinaryReader br = new BinaryReader((new FileStream("d:\\binaryfile", FileMode.Open, FileAccess.Read)));
            i = br.ReadInt32();
            Console.WriteLine("Reading :i     "+i);
            d = br.ReadDouble();
            Console.WriteLine("Reading :d    "+d);
            b = br.ReadBoolean();
            Console.WriteLine("Reading :b    "+b);
            br.Close();
            Console.ReadLine();
        }
    }
}

DirectoryInfo and FileInfo classes:
These classes are required to implement the windows file system.
DirectoryInfo class which contains properties like Attribure, CreationTime, Exist, Extention, FullName, LastAccessTime, Name.
Methods are
1.  Create()
2.  CreateSubDirectory()
3.  Delete()
4.  GetDirectories()
5.  GetFiles()

FileInfo class has properties like Attribure, CreationTime, Exist, Extention, FullName, LastAccessTime, Name.
Methods are
1.  Create()
2.  AppedText()
3.  Delete()
4.  Open()
5.  OpenRead()

using System;
using System.IO;
namespace directoryfileinfo
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo mydir = new DirectoryInfo("D:\\");
            FileInfo[]  myfile = mydir.GetFiles();
            foreach (FileInfo file in myfile)
            {
                Console.WriteLine("File Name :  {0}, File Size :{1}",file.Name,file.Length);
                Console.ReadLine();
            }
        }
    }
}

Properties:

In order to validate the user input, we require properties with the help of Get and Set accessories.
Set accessory is required for setting the value after validating a user input.
Get accessory is required for returning the value.

Types of Properties:
1.  ReadOnly Property.
2.  ReadWrite Property.
3.  WriteOnly Property.

ReadOnly property will support only Get.
WriteOnly property wil support only Set.
ReadWrite property will support both Get and Set.



Syntax:
Private datatype fieldname;
Public datatype propertyname
{
       get
       {
              return fieldname;
       }
       set
       {
              fieldname=value;
       }                          |
}                            keyword

using System;
namespace PropertiesGetSet
{
    public class Student
    {
        private int marks;
        public int Marks
        {
            get
            {
                return marks;
            }
            set
            {
                if(value >=0 && value <=100)
                     marks=value;
                else
                     marks=0;            
            }
        }
        public void Display()
        {
            Console.WriteLine("the marks of student scored are: {0}",Marks);
        }

    }
    class Program
    {
static void Main(string[] args)
       o; {
            Student stu = new Student();
            stu.Marks = 56;
            stu.Display();
            Console.ReadLine();
        }
    }

Comments

Popular posts from this blog

what is Event Cache table in sharepoint

CAML Query syntax and options in SharePoint

Change anchor link url in sharepoint calender