file handling ppt gniit
DESCRIPTION
file handling presentation GNIITTRANSCRIPT
File Handling
ISAS Presentation
Submitted To: Mr. Ankur SirSubmitted By: Anil Ashokbhai Nikam
What is File?
File is collection of data stored on a disk with specific name and directory path.
It is the a stream of characters or a flow of related data.
Streams read/write bytes to/from Storage device
Types Of Files
What is File Handling?
It is the concept of the File input & output operation.
Its explains the process of reading and writing in the files and the binary files.
Its play very important role in Data storing and retriving from a file.
Examples of File Handling
Microsoft Word•For Creating File or Editing and Deleting Data from the Document files(.Doc)
NotePad•For Creating File or Editing and Deleting Data from the Text files (.txt)
Photoshop•For Creating File or Editing and Deleting Data from the Image files (.psd, .gif, .jpg, .jpeg, .png, .bmp etc..)
Need Of File Handling In C#
To store useful data taken from User by the C# application & store it permanently in a file in a storage device for futher use.
It can also performs process creating, editing or deleting file.
By this we can retrive data or insert it and perform different operation on data and store final output in file.
System.IO namespace use for file handling
Classes needed while performing operations
System.IO(Namespace)
FileStream
StreamReader
StreamWriter
BinaryReader
BinaryWriter
FileStream Class using Enumerator
FileMode•Append•Create•CreateNew•Open•OpenOrCreate•Truncate
FileAccess
•Read•Write•ReadWrite
FileShare
•Inheritable•None•Read•ReadWrite•Write
To Open an existing file or to create a new file , we need an object of FileStream Class
SyntaxFileStream <object name> = new
FileStream(<file name> , <FileMode> , <FileAccess> , <FileShare>);
Code SnippetFileStream f = new FileStream(“C.txt” ,
FileMode.Open , FileAccess.Read , FileShare.Read);
Stream Classes
Close
• Close the object of that class & Stream.
Peek
• Returns the next available character but does not consume it.
Read
• Read the next character.
ReadLine
• Read a line of a character.
Seek
• Allows the Read/Write Postion to moved to any position within file.
Close
• Closes the current object & Stream.
Flush
• Clear all bufers for the current writer.
Write
• Write to the Stream.
WriteLine• Write data specified by the
overloaded parameters followed by end of the line.
StreamReader StreamWriter
StreamReader Syntax
FileStream fs = new FileStream(“MyFile.txt”, FileMode.Open, FileAccess.Read);StreamReader sr = new StreamReader(fs);sr.BaseStream.Seek(0, SeekOrigin.Begin);string str = sr.ReadLine();
StreamWriter SyntaxFileStream fs = new FileStream(“MyFile.txt”, FileMode.Append, FileAccess.Write);StreamWriter w = new StreamWriter(fs);string str = w.ReadLine();
Example of StreamReader using System;using System.IO; class FileRead{ public void ReadData() {
FileStream fs = new FileStream(“Myfile.txt”,FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);sr.BaseStream.Seek(0, SeekOrigin.Begin);string = 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();
}}
Example of StreamWriter
using System.IO; class Program{ public static void Main(String[] args) {
FileStream fs = new FileStream(“MyFile.txt”, FileMode.Append, FileAccess.Write);StreamWriter w = new StreamWriter(fs);
Console.WriteLine(“Enter the String”);string sr = Console.ReadLine();w.write(str);w.Flush();w.Close();fs.Close();
}}
Reading & Writing in Binary Files
•Close the current reader & stream.
Close
•Read characters from the Stream & advance current postion of stream.
Read
•Close the current binary object & stream.close
•Sets the position within the current stream.Seek
•Write a value to the current streamWrite
•Clear all buffer for the current writer and causes any bufferes data to be written in the deviceFlush
BinaryReader Class
BinaryWriter Class
BinaryReader Syntax
FileStream fs = newFileStream(@”C:\Test.DAT”, FileMode.Open, FileAcces.Read);BinaryReader br = new BinaryReader(fs);
BinaryWriter Syntax
FileStream fs = newFileStream(@”C:\Test.DAT”, FileMode.Open, FileAcces.Read);BinaryWriter bw = new BinaryReader(fs);
Example of BinaryFile
Visual Studio can open binary files and you can look at the data. It won't be readable normally, but it gives you an idea what is happening
Questions?
Thank you