YOMEDIA
ADSENSE
Lecture Windows programming: Chapter 8 - Châu Thị Bảo Hà
49
lượt xem 3
download
lượt xem 3
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
Lecture Windows programming - Chapter 8 introduce to the files and streams. In this chapter you will be learn contents: Files and Streams overview, IO namespace and its classes, serialization.
AMBIENT/
Chủ đề:
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Lecture Windows programming: Chapter 8 - Châu Thị Bảo Hà
- Files & Streams Chapter 8 Ebook: Beginning Visual C# 2010, chapter 21 Reference: C# How to Program, chapter 17
- Contents Files and Streams overview IO namespace and its classes Serialization
- Files and Streams Files and Streams C# views each file as a sequential stream of bytes each file ends either with: an end-of-file marker specific byte number recorded in a system-maintained administrative data structure When a file is opened, an object is created and a stream is associated with the object when a file is opened two types of streams output input
- Contents Files and Streams IO namespace and its classes Serialization
- IO namespace and its classes Class Description Stream Provides a view of the sequence of bytes File Provides static methods for creating and manipulating files FileSystemIn Contains methods that are common to file and directory fo manipulation Directory Provides methods for creating, moving and enumerating through directories and sub-directories DirectoryInfo Provides instance methods for creating, moving and enumerating directories and sub-directories TextReader Represents a reader that can read a sequence of characters TextWriter Represents a writer that can write a sequence of characters BinaryReade Reads primitive data types as binary values in a specific r encoding BinaryWriter Writes primitive types in binary to a stream and supports
- Stream classes An abstract class from which different classes are derived MemoryStream used to read and write data to memory BufferedStream used to read and write to the buffer FileStream used to perform read and write operations on files NetworkStream used to send and receive data across the network CryptoStream used to link the stream of data to any cryptography object for the purpose of encrypting the data
- FileStream class constructors Constructors Description FileStream(string Takes in the path of the file to be FilePath, FileMode) read from or written to and any one of the FileMode enumerator values as its arguments FileStream(string Takes in the path of the file to be FilePath, FileMode, read from or written to, any one of FileAccess) the FileMode enumerator values and FileAccess enumerator values as it arguments
- Enumerators used with FileStream class FileMode Enumerators FileAccess Append Enumerators Create Read CreateNew Write Open ReadWrite OpenOrCreate Truncate
- Directory and File classes The directory class contains static methods that help in handling directories and subdirectories The file class contains static methods that help in handling files it also helps in the creation of FileStream class
- File methods Methods Description Copy( string Used to copy the contents of a source file to a SourcePath, string destination file in the specified path. If the DestinationPath ); destination file does not exist, a new file is created with the specified name in the specified path Create( string Path ) Used to create a file with the specified name in the specified path Delete( string Path ) Used to delete a file from a specified path Exists( string Path ) Used to verify whether a file with the specified name exists in the specified path. It returns a Boolean value Move( string Used to move the specified file from the SourcePath, string source location to the destination location DestinationPath )
- Directory methods Methods Description CreateDirectory (String Creates all the directories in the specified Path) path Delete( string Path ) Deletes an empty directory from a specified path Exists( string Path ) Checks whether the given path refers to an existing directory on disk Move( string Used to move the specified file from the SourceFilePath, string source location to the destination location DestinationFilePath ) GetCurrentDirectory() Returns the current working directory GetDirectories ( string Gets the names of subdirectories Path) GetFiles ( string Path) Gets the names of files in a specified directory
- Example of Directory and File using System; using System.IO; class DirectoryDemo { static void Main(string[] args) { Directory.CreateDirectory ("Testdir"); File.Copy ("D:\\abc.txt", "Testdir\\abc.txt"); Console.WriteLine("File Content Copied"); } }
- Example of Directory and File void CreateFile() { string path = @"c:\temp\MyTest.txt"; if ( Directory.Exists( @"c:\temp" ) ) { // create and save file… } } void ReadFile() { string path = @"c:\temp\MyTest.txt"; if ( File.Exists( path ) ) { // read file… } }
- TextWriter class It is an abstract base class for classes that can be used to write sequential characters The StreamWriter and StringWriter classes are two of the derived classes of the TextWriter class the StreamWriter writes characters to a stream in a specified encoding the StringWriter class is used to write data to a string
- StreamWriter methods Method Description Write() Used to write a character from the stream and move the current position to the next character WriteLine() Writes a sequence of a line of characters to the stream. It adds a line terminator to mark the end of the string
- StreamWriter example static void Main(string[] args) { string path = @"d:\myFile\TestFile.txt"; if (Directory.Exists(path.Substring(0, path.LastIndexOf(@"\")))) { StreamWriter sw = new StreamWriter(path); // Add some text to the file sw.Write("This is the "); sw.WriteLine("header for the file."); sw.WriteLine(" ---------------------- "); sw.Write("The date is: "); sw.WriteLine(DateTime.Now); sw.Flush(); sw.Close(); Console.Write("Write file success"); } else Console.Write("Can not create file. The directory not exists."); }
- TextReader class It is an abstract base class for classes that can be used to read a sequential series of characters the StreamReader and StringReader classes are two of the derived classes of the TextReader class the StreamReader reads a character in a byte stream and converts it to the specified encoding the StringReader class is used to read data from an input string
- StreamReader methods Methods Description Read() Used to read a character from the byte stream and move the current position to the next character ReadLine() Reads a sequence of, or a line of character from the byte stream and outputs a string data ReadToEnd() Used to read the byte stream from the current position in the stream to the end of the stream
- StreamReader example static void Main(string[] args) { string path = @"d:\myFile\TestFile.txt"; if (File.Exists(path)) { StreamReader sr = File.OpenText(path); String input; while ((input = sr.ReadLine()) != null) { Console.WriteLine(input); } sr.Close(); } else Console.Write("Can not read the file. The file not exists."); }
- BinaryWriter class It is used for writing binary data from a C# variable to a specified stream The most commonly used methods of this class are Close() and Write() methods
ADSENSE
CÓ THỂ BẠN MUỐN DOWNLOAD
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
AANETWORK
TRỢ GIÚP
HỖ TRỢ KHÁCH HÀNG
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn