file handling ppt gniit

20
File Handling ISAS Presentation Submitted To: Mr. Ankur Sir Submitted By: Anil Ashokbhai Nikam

Upload: anil293

Post on 27-Dec-2015

25 views

Category:

Documents


2 download

DESCRIPTION

file handling presentation GNIIT

TRANSCRIPT

Page 1: File Handling ppt GNIIT

File Handling

ISAS Presentation

Submitted To: Mr. Ankur SirSubmitted By: Anil Ashokbhai Nikam

Page 2: File Handling ppt GNIIT

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

Page 3: File Handling ppt GNIIT

Types Of Files

Page 4: File Handling ppt GNIIT

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.

Page 5: File Handling ppt GNIIT

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..)

Page 6: File Handling ppt GNIIT

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

Page 7: File Handling ppt GNIIT

Classes needed while performing operations

System.IO(Namespace)

FileStream

StreamReader

StreamWriter

BinaryReader

BinaryWriter

Page 8: File Handling ppt GNIIT

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

Page 9: File Handling ppt GNIIT

SyntaxFileStream <object name> = new

FileStream(<file name> , <FileMode> , <FileAccess> , <FileShare>);

Code SnippetFileStream f = new FileStream(“C.txt” ,

FileMode.Open , FileAccess.Read , FileShare.Read);

Page 10: File Handling ppt GNIIT

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

Page 11: File Handling ppt GNIIT

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();

Page 12: File Handling ppt GNIIT

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){

Page 13: File Handling ppt GNIIT

Console.WriteLine(“{0}, str);str = sr.ReadLine();

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

} public static void Main(String[] args)

{FileRead fr = new FileRead();fr.ReadData();

}}

Page 14: File Handling ppt GNIIT

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);

Page 15: File Handling ppt GNIIT

Console.WriteLine(“Enter the String”);string sr = Console.ReadLine();w.write(str);w.Flush();w.Close();fs.Close();

}}

Page 16: File Handling ppt GNIIT

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

Page 17: File Handling ppt GNIIT

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);

Page 18: File Handling ppt GNIIT

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

Page 19: File Handling ppt GNIIT

Questions?

Page 20: File Handling ppt GNIIT

Thank you