I/O Operations with File Systems

File Input And Output

          The file is a collection of data stored on a disk with a specific name and very often with a directory path.
          When we open the file for reading data or writing data, it becomes a stream.
          Stream is a sequence of bytes travelling from source to a destination over a communication path.
          The operations use the namespace “System.IO”

Two basic streams are:

        Input Stream: used for a read operation
        Output Stream: used for performing a write operation.

Class Name
Description
FileStream
Used to read from and write to any location within a file.
BinaryReader
Used to read primitive data types from a binary stream.
BinaryWriter
Used to write primitive data types in binary format.
StreamReader
Used to read characters from a byte stream.
StreamWriter
Used to write characters to a stream.
StringReader
Used to read from a string buffer.
StringWriter
Used to write into a string buffer.
DirectoryInfo
Used to perform operations on directories.
FileInfo
Used to perform operations on files.

FileStream Class

          We can use the FileStream class in the System.IO namespace to read from, to write and to close files.
          This class inherits from the abstract class “Stream”.

FileStream = new FileStream(,
, ,)

FileMode Enumerator

Defines various methods for opening files.
FileMode
Functionality
Append
Opens the file if it exists and places the cursor at the end of the file or
creates new line
Create
Creates a new file.
CreateNew
Specifies to an operating system that is should create a new file.
Open
Opens an existing file.
OpenOrCreate
Specifies to the operating system that it should open a file if it exists,
 else create new file.
Truncate
Opens an existing file and truncate it to zero bytes.

File Access Enumerator

This enumerator indicates wheather you want to read data from the file, write to the file, or perform both the operations.
The member of the FileAccess enumerator are:
          Read
          ReadWrite
          Write

FileShare Enumerator

FileShare
Functionality
Inheritable
Allows a file handle to pass inheritance to the child processes.
None
Declines sharing of a current file.
Read
Allows opening of a file for reading purpose.
ReadWrite
Allows opening a file for the purpose of reading and writing.
Write
Allows opening of a file for writing purpose.

StreamReader Class

FileStream fs = new FileStream(“C://Myfile.txt”,FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs);

Methods
Description
Close
Closes the object of StreamReader class and the underlying stream, and releases any system resources associated with the reader.
Peek
Returns the next available character but does not consume it.
Read
Reads the next character or the next set of character but does not consume it.
ReadLine
Reads a line of characters from the current stream and returns data as a string.
Seek
Allows the read/write position to be moved to any position within the file.

using System;
using System.IO;
Class FileRead
{
            public void ReadData()
            {
                        FileStream fs = new FileStream(“C://Myfile.txt”,FileMode.Open,FileAccess.Read);
                        StreamReader sr = new StreamReader(fs);
                        sr.BaseStram.Seek(0,SeekOrigin.Begin);
                        string str = sr.ReadLine();
                        while(str!=null)
                        {
                                    Console.WriteLine(“{0}”,str);
                                    str = sr.ReadLine();
                        }
                        sr.Close();
                        fs.Close();
            }
            public static void Main(String[] args)
            {
                        FileRead fr = new FileRead();
                        fr.ReadData();
            }
}

StreamWritter class

The StreamWriter class is inherited from the abstract class TextWritter.

Methods
Description
Close
Closes the current StreamWriter object and the underlying stream.
Flush
Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.
Write
Writes to the stream.
WriteLine
Writes data specific by the overloaded parameters followed by end of line.

using System;
using System.IO;
Class FileWrite
{
            public void WriteData()
            {
                        FileStream fs = new FileStream(“C://Myfile.txt”,FileMode.Open,FileAccess.Write);
                        StreamWriter w = new StreamWriter(fs);
                        Console.WriteLine(“Enter a string”);
                        string str = Console.ReadLine();
                        w.Write(str);
                        w.Flush();
                        w.Close();
                        fs.Close();
            }
            public static void Main(String[] args)
            {
                        FileWrite fw = new FileWrite();
                        fw.WriteData();
            }
}

DirectoryInfo Class

Property
Description
Attributes
Gets or sets the attributes associated with the file. This property is inherited from FileSystemInfo.
Creationtime
Gets or sets the creation time of the file.
Exists
Gets a boolean value indicating whether the directory exists or not.
Extension
Gets a string containing the file extension.
FullName
Gets a string containing full path of the directory.
LastAccessTime
Gets a string containing time of the directory.
Name
Gets a string containing the name of a given file.
Create
Create a directory.
CreateSubdirectory
Creates a directory.
Delete
Delete a directory.

FileInfo Class

Property
Description
Attributes
Gets or sets attributes associated with the file. This property is inherited from File SystemInfo.
CreationTime
Gets or sets Creation time of the current file.
Directory
Gets an instance of the directory which the file belongs to.
Exists
Gets a boolean indicating whether the file exists or not.
Extension
Gets a string containing the file extention.
FullName
Gets a string containing the full path of the file.
LastAccessTime
Gets the last accessed time of the file.
LastWriteTime
Gets the time of the last written activity of the file.
Length
Gets the size of the file.
Name
Gets a string contatining the name of a given file.
Create
Creates a file.
AppendText
Appends a text to the file represented by the FileInfo object
Delete
Deletes a file.
Open
Opens file.
OpenRead
Opens a file in read-only mode.



using System;
using System.IO;
Class Tester
{
            public static void Main()
            {
                        DirectoryInfo mydir = new DirectoryInfo(@”c:\Windows”);
                        FileInfo[] FileInDir = mydir.GetFiles();
                        foreach(FileInfo file in FileInDir)
                        {
                                    Console.WriteLine(“File Name: {} Size: {1} bytes”,file.Name,file.Length);
                        }
            }
}

              _____________________________________________________________________
Reach us At: - 0120-4029000; 0120-4029024; 0120-4029025, 0120-4029027; 0120-4029029
Mbl: 9953584548
Write us at: - Smruti@apextgi.com and pratap@apextgi.com

No comments:

Post a Comment