Creation/opening, Reading data, writing data, seeking/positioning, closing/disconnecting.
Creation
Để tạo hoạt mở một stream có nghĩa là để kết nối một stream tới một nguồn dữ liệu (data source), một cơ chế cho truyền dữ liệu or stream khác.
Ví dụ: Khi chúng ta có một file stream, thì chúng ta truyền file name and file mode (reading, writing, reading and writing đồng thời).
Reading
Trích xuất dữ liệu từ một stream. Reading luôn luôn thực hiện tuần tự từ vị trí hiện tại ở stream.
Reading là hoạt động theo block, và nếu bên kia đã không gửi dữ liệu trong khi chúng ta cố gắng để đọc hoặc các dữ liệu được gửi chưa đến có thể xảy ra một sự delay (vài mili giây đến giờ, ngày hoặc hơn).
e.g. Khi đọc 1 luồng dữ liệu từ mạng có thể chậm lại vì các mạng or bên khác có thể không gửi bất kỳ dữ liệu nào.
Reading là hoạt động theo block, và nếu bên kia đã không gửi dữ liệu trong khi chúng ta cố gắng để đọc hoặc các dữ liệu được gửi chưa đến có thể xảy ra một sự delay (vài mili giây đến giờ, ngày hoặc hơn).
e.g. Khi đọc 1 luồng dữ liệu từ mạng có thể chậm lại vì các mạng or bên khác có thể không gửi bất kỳ dữ liệu nào.
Writing
Gửi dữ liệu đến stream theo một cách cụ thể nào đó, thực hiện từ vị trí hiện tại của stream.
Có thể có khả năng hoạt động theo block, trước khi dữ liệu được gửi trên đường.
e.g. Nếu bạn gửi một dữ liệu khổng lồ qua một network stream, hoạt động có thể chậm lại trong khi dữ liệu đang di chuyển trên mạng.
Positioning
(Positioning) Đặt vị trí hoặc (seeking) tìm kiếm trong stream là di chuyển vị trí hiện tại.
Di chuyển được thực hiện theo các vị trí hiện tại, nơi mà chúng có thể đặt vào vị trí theo vị trí hiện tại, bắt đầu của một stream, hoặc kết thúc của một stream.
Di chuyển có thể được thực hiện chỉ trong stream hỗ trợ đặt vị trí.
Closing
Để Close or ngắt kết nối một stream là hoàn thành công việc với một stream và giải phóng tài nguyên đã được chiếm.
Closing phải được diễn ra càng sớm càng tốt sau khi stream đã được phục vụ mục đích của nó, vì tài nguyên đã được mở bời người dùng thưởng không thể được sử dụng bởi người dùng khác (bao gồm cả các chương trình khác trên cùng một máy chạy song song với chương trình của chúng ta).
Stream in .NET
Lớp làm việc với stream được đặt trong namespace System.IO (BufferedStream, FileStream, MemoryStream, GZipStream, NetworkStream)
Có 2 kiểu stream chinh là: binary data and text data
Binary streams
Làm việc với dữ liệu nhị phân (raw).
Bạn có thể đoán rằng đó làm cho họ tổng quát, Chúng cỏ thể được sử dụng để đọc thông tin từ tất cả các loại tập tin (hình ảnh, âm nhạc, tập tin đa phương tiện, file văn bản..). Chúng ta sẽ có một cái nhìn ngắn gọn qua nó, vì chúng ta đang tập trung vào làm việc với các tập tin văn bản (text file).
Lớp chính chúng ta sử dụng để đọc, ghi binary stream: FileStream, BinaryReader and BinaryWriter.
FileStream: reading and writing from a binary file (read/write one byte available of bytes)...
BinaryWriter: Writes primitive types and binary values. Write(..) method cho phép ghi bất kỳ các kiểu cơ bản nào (integers, characters, Booleans, arrays, string and more.
BinaryReader: Read primitive data types and binary values.
Text streams
Rất giống với Binary nhưng chỉ làm việc với text data, char, string.
2 Lớp chính TextReader and TextWriter: có các method quan trọng:
- Readline();
- ReadToEnd();
- Write();
- WriteLine();
StreamReader and StreamWriter hay được dùng, nó kế thừa thừ lớp TextReader and TextWriter.
Reading from a Text File
Open a text file
// Create a StreamReader connected to a file
StreamReader reader = new StreamReader("file1.txt");
// Read the file here..
int lineNumber = 0;
// Read first line from the text file
string line = reader.ReadLine();
// Read the other lines from the text file
while (line != null)
{
lineNumber++;
Console.WriteLine("Line {0}: {1}", lineNumber, line);
line = reader.ReadLine();
}
// Close the reader resource after you've finished using it
reader.Close();
Auto Closing of the Stream after working with it
// Create an instance of StreamReader to read from a file
Streamreader reader = new StreamReader("file1.txt");
using (reader)
{
int lineNumber = 0;
// Read first line from the text file
string line = reader.ReadLine();
// Read the other lines from the text file
While (line != null)
{
l ineNumber++;
Console.WriteLine("Line {0}: {1}", lineNumber, line);
line = reader.ReadLine();
}
}
File Encodings. Reading in Cyrillic
Character Encodings
UTF-8, Windows-1251
System.Text.Encoding win1251 = Encoding.GetEncoding("Windows-1251");
System.Text.Encoding utf8 = Encoding.GetEncoding("UTF-8");
Reading a Cyrillic Content
StreamReader sr1 = new StreamReader("file1.txt", win1251);
StreamReader sr2 = new StreamReader("file1.txt", utf8);
Writing to a text file
// Create a StreamWriter instance
StreamWriter writer = new StreamWriter("file2.txt", Encoding.GetEncoding("UTF-8"));
{
Console.WriteLine("Can not find file {0}.", fileName);
}
catch (DirectoryNotFoundException)
{
Console.WriteLine(""Invalid directory in the file path.");
}
catch (IOException)
{
Console.WriteLine("Can not open the file {0}.", fileName);
}
Gửi dữ liệu đến stream theo một cách cụ thể nào đó, thực hiện từ vị trí hiện tại của stream.
Có thể có khả năng hoạt động theo block, trước khi dữ liệu được gửi trên đường.
e.g. Nếu bạn gửi một dữ liệu khổng lồ qua một network stream, hoạt động có thể chậm lại trong khi dữ liệu đang di chuyển trên mạng.
Positioning
(Positioning) Đặt vị trí hoặc (seeking) tìm kiếm trong stream là di chuyển vị trí hiện tại.
Di chuyển được thực hiện theo các vị trí hiện tại, nơi mà chúng có thể đặt vào vị trí theo vị trí hiện tại, bắt đầu của một stream, hoặc kết thúc của một stream.
Di chuyển có thể được thực hiện chỉ trong stream hỗ trợ đặt vị trí.
Closing
Để Close or ngắt kết nối một stream là hoàn thành công việc với một stream và giải phóng tài nguyên đã được chiếm.
Closing phải được diễn ra càng sớm càng tốt sau khi stream đã được phục vụ mục đích của nó, vì tài nguyên đã được mở bời người dùng thưởng không thể được sử dụng bởi người dùng khác (bao gồm cả các chương trình khác trên cùng một máy chạy song song với chương trình của chúng ta).
Stream in .NET
Lớp làm việc với stream được đặt trong namespace System.IO (BufferedStream, FileStream, MemoryStream, GZipStream, NetworkStream)
Có 2 kiểu stream chinh là: binary data and text data
Binary streams
Làm việc với dữ liệu nhị phân (raw).
Bạn có thể đoán rằng đó làm cho họ tổng quát, Chúng cỏ thể được sử dụng để đọc thông tin từ tất cả các loại tập tin (hình ảnh, âm nhạc, tập tin đa phương tiện, file văn bản..). Chúng ta sẽ có một cái nhìn ngắn gọn qua nó, vì chúng ta đang tập trung vào làm việc với các tập tin văn bản (text file).
Lớp chính chúng ta sử dụng để đọc, ghi binary stream: FileStream, BinaryReader and BinaryWriter.
FileStream: reading and writing from a binary file (read/write one byte available of bytes)...
BinaryWriter: Writes primitive types and binary values. Write(..) method cho phép ghi bất kỳ các kiểu cơ bản nào (integers, characters, Booleans, arrays, string and more.
BinaryReader: Read primitive data types and binary values.
Text streams
Rất giống với Binary nhưng chỉ làm việc với text data, char, string.
2 Lớp chính TextReader and TextWriter: có các method quan trọng:
- Readline();
- ReadToEnd();
- Write();
- WriteLine();
StreamReader and StreamWriter hay được dùng, nó kế thừa thừ lớp TextReader and TextWriter.
Reading from a Text File
Open a text file
// Create a StreamReader connected to a file
StreamReader reader = new StreamReader("file1.txt");
// Read the file here..
int lineNumber = 0;
// Read first line from the text file
string line = reader.ReadLine();
// Read the other lines from the text file
while (line != null)
{
lineNumber++;
Console.WriteLine("Line {0}: {1}", lineNumber, line);
line = reader.ReadLine();
}
// Close the reader resource after you've finished using it
reader.Close();
Auto Closing of the Stream after working with it
// Create an instance of StreamReader to read from a file
Streamreader reader = new StreamReader("file1.txt");
using (reader)
{
int lineNumber = 0;
// Read first line from the text file
string line = reader.ReadLine();
// Read the other lines from the text file
While (line != null)
{
l ineNumber++;
Console.WriteLine("Line {0}: {1}", lineNumber, line);
line = reader.ReadLine();
}
}
File Encodings. Reading in Cyrillic
Character Encodings
UTF-8, Windows-1251
System.Text.Encoding win1251 = Encoding.GetEncoding("Windows-1251");
System.Text.Encoding utf8 = Encoding.GetEncoding("UTF-8");
Reading a Cyrillic Content
StreamReader sr1 = new StreamReader("file1.txt", win1251);
StreamReader sr2 = new StreamReader("file1.txt", utf8);
Writing to a text file
// Create a StreamWriter instance
StreamWriter writer = new StreamWriter("file2.txt", Encoding.GetEncoding("UTF-8"));
// Ensure the writer will be closed when no longer used
using (writer)
{
// Loop through the numbers from 1 to 20 and write them
for (int i = 1; i < 20; i++)
{
writer.WriteLine(i);
}
}
Input / Output Exception Handling
string fileName = @"somedir\somefile.txt";
try
{
}
catch (FileNotFoundException){
Console.WriteLine("Can not find file {0}.", fileName);
}
catch (DirectoryNotFoundException)
{
Console.WriteLine(""Invalid directory in the file path.");
}
catch (IOException)
{
Console.WriteLine("Can not open the file {0}.", fileName);
}
0 nhận xét:
Đăng nhận xét