Download presentation
Presentation is loading. Please wait.
1
How to work with files and data streams
Murach Chapter 21
2
Objectives Applied Develop an application that requires the use of text or binary files. Knowledge Distinguish between a text file and a binary file. Describe the use of FileStream StreamReader StreamWriter BinaryReader BinaryWriter objects. Describe two common types of I/O exceptions.
3
Main Sistem.IO File Classes and Operations
4
System.IO classes used to work with drives and directories
Using System.IO; System.IO classes used to work with drives and directories Code that uses some of the Directory methods string dir 2015\Files\"; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } Code that uses some of the File methods string path = dir + "Products.txt"; if (File.Exists(path)) File.Delete(path);
5
Very Common System.IO Methods
Exists(path) Delete(path) Copy(source, destination) Move(Source, destination)
6
A text file displayed in a text editor (.txt)
Text and binary files A text file displayed in a text editor (.txt) A binary file displayed in a text editor (.dat) (ProductMaintenance) public class ProductDB private const string dir 2015\Files\"; private const string path = dir + "Products.dat";
7
Two types of Files and Streams
8
System.IO classes used to work with files and streams
9
The syntax for creating a FileStream object
10
FileShare Enumeration
Member name Description Delete Allows subsequent deleting of a file. Inheritable Makes the file handle inheritable by child processes. This is not directly supported by Win32. None Declines sharing of the current file. Any request to open the file (by this process or another process) will fail until the file is closed. Read Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file. ReadWrite Allows subsequent opening of the file for reading or writing. If this flag is not specified, any request to open the file for reading or writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file. Write Allows subsequent opening of the file for writing. If this flag is not specified, any request to open the file for writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
11
System.IO classes used to work with files and streams
12
Code that creates a FileStream object for writing
string path 2015\Files\Products.txt"; FileStream fs = new FileStream( path, FileMode.Create, FileAccess.Write); Code that creates a new FileStream object for reading path, FileMode.Open, FileAccess.Read); string path "; - will be in bin string path - will be next to sources Just because you added a file (e.g. txt) to your solution doesn't mean the file is placed into your output bin/debug directory. If you want to use relative path, make sure your text file is copied during build to the output directory. To do this, in solution explorer go to properties of the text file and set Copy to Output Directory to Always or Copy if newer.
13
The exception classes for file I/O
14
Code that uses exception classes
string dirPath 2015\Files\"; string filePath = dirPath + "Products.txt"; FileStream fs = null; try { fs = new FileStream(filePath, FileMode.Open); // code that uses the file stream // to read and write data from the file } catch(FileNotFoundException) MessageBox.Show(filePath + " not found.", "File Not Found"); catch(DirectoryNotFoundException) MessageBox.Show(dirPath + " not found.", "Directory Not Found"); catch(IOException ex) MessageBox.Show(ex.Message, "IOException"); finally if (fs != null) fs.Close();
15
The basic syntax for creating a StreamWriter object
16
Code that writes data from a collection of Product objects to a text file
StreamWriter textOut = new StreamWriter( new FileStream( path, FileMode.Create, FileAccess.Write)); foreach (Product product in products) { textOut.Write(product.Code + "|"); textOut.Write(product.Description + "|"); //same line as above textOut.WriteLine(product.Price); } textOut.Close();
17
The basic syntax for creating a StreamReader object
18
Code that reads data from a text file into a collection of Product objects
StreamReader textIn = new StreamReader( new FileStream( path, FileMode.OpenOrCreate, FileAccess.Read)); List<Product> products = new List<Product>(); while (textIn.Peek() != -1) { string row = textIn.ReadLine(); string[] columns = row.Split('|'); Product product = new Product(); product.Code = columns[0]; product.Description = columns[1]; product.Price = Convert.ToDecimal(columns[2]); products.Add(product); } textIn.Close();
19
Again
20
A class that works with a text file
using System; using System.IO; using System.Collections.Generic; namespace ProductMaintenance { public static class ProductDB private const string dir 2015\Files\"; private const string path = dir + "Products.txt"; public static List<Product> GetProducts() // if the directory doesn't exist, create it if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); // create the object for the input stream for a text file StreamReader textIn = new StreamReader( new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));
21
A class that works with a text file cont.
// create the list List<Product> products = new List<Product>(); // read the data from the file and store it in the list while (textIn.Peek() != -1) { string row = textIn.ReadLine(); string[] columns = row.Split('|'); Product product = new Product(); product.Code = columns[0]; product.Description = columns[1]; product.Price = Convert.ToDecimal(columns[2]); products.Add(product); } // close the input stream for the text file textIn.Close(); return products;
22
A class that works with a text file (Save) cont.
public static void SaveProducts(List<Product> products) { // create the output stream for a text file that exists StreamWriter textOut = new StreamWriter( new FileStream(path, FileMode.Create, FileAccess.Write)); // write each product foreach (Product product in products) textOut.Write(product.Code + "|"); textOut.Write(product.Description + "|"); textOut.WriteLine(product.Price); } // close the output stream for the text file textOut.Close();
23
The basic syntax for creating a BinaryWriter object
24
The basic syntax for creating a BinaryWriter object cont
25
Code that writes data from a collection of Product objects to a binary file
BinaryWriter binaryOut = new BinaryWriter( new FileStream(path, FileMode.Create, FileAccess.Write)); foreach (Product product in products) { binaryOut.Write(product.Code); binaryOut.Write(product.Description); binaryOut.Write(product.Price); } binaryOut.Close();
26
The basic syntax for creating a BinaryReader object
27
Code that reads data from a binary file into a collection of Product objects
BinaryReader binaryIn = new BinaryReader( new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)); List<Product> products = new List<Product>(); while (binaryIn.PeekChar() != -1) { Product product = new Product(); product.Code = binaryIn.ReadString(); product.Description = binaryIn.ReadString(); product.Price = binaryIn.ReadDecimal(); products.Add(product); } binaryIn.Close();
28
A class that works with a binary file
public class ProductDB { private const string dir 2015\Files\"; private const string path = dir + "Products.dat"; public static List<Product> GetProducts() // if the directory doesn't exist, create it if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); // create the object for the input stream for a binary file BinaryReader binaryIn = new BinaryReader( new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));
29
A class that works with a binary file cont
// create the array list List<Product> products = new List<Product>(); // read the data from the file and store it in the List<Product> while (binaryIn.PeekChar() != -1) { Product product = new Product(); product.Code = binaryIn.ReadString(); product.Description = binaryIn.ReadString(); product.Price = binaryIn.ReadDecimal(); products.Add(product); } // close the input stream for the binary file binaryIn.Close(); return products;
30
A class that works with a binary file cont
public static void SaveProducts(List<Product> products) { // create the output stream for a binary file that exists BinaryWriter binaryOut = new BinaryWriter( new FileStream(path, FileMode.Create, FileAccess.Write)); // write each product foreach (Product product in products) binaryOut.Write(product.Code); binaryOut.Write(product.Description); binaryOut.Write(product.Price); } // close the output stream binaryOut.Close();
31
Project 5-1 Maintain student scores (text file)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.