C Stream Class FileStream Learning Use Notes
- 2021-06-28 13:46:34
- OfStack
static void Main(string[] args)
{
// Establish FileStream Object needs to be specified File name, open mode, access mode, share mode
//1. Call its own constructor to create a stream
FileStream fs1 = new FileStream(@"F:\1.txt", FileMode.Open, FileAccess.ReadWrite,FileShare.ReadWrite);
//2. utilize File Class or FileInfo Class Open Method
//FileStream fs2 = File.Open(@"F:\1.txt", FileMode.Open, FileAccess.Read);
////3. utilize File Class or FileInfo Class Create Method Creation
//FileStream fs3 = File.Create(@"F:\1.txt");//FileMode.Create,FileAccess.Write
////4. utilize File Class or FileInfo Class OpenRead Method Creation
//FileStream fs4 = File.OpenRead(@"F:\1.txt");//FileMode.Open, FileAccess.Read
////5. utilize File Class or FileInfo Class OpenWrite Method Creation
//FileStream fs5 = File.OpenWrite(@"F:\1.txt");//FileMode.Open,FileAccess.Write
//InsertText(fs1, " Hello, I'm a rookie rohelm.X!");// I'm looking for it here 1 Experimenting with read-write shared streams
//fs1.Close();// Although this is FileShare.ReadWrite Mode, but additional privileges are required, so releasing the stream will release the file
// Console.WriteLine( File.ReadAllText(@"F:\1.txt"));
Console.WriteLine(ReadStream(fs1));
Console.ReadKey();
}
// The process of writing to the stream is the process of encoding, i.e. 1 group unicode Character Conversion 1 Byte Sequence
public static void InsertText(FileStream fs, string str)
{
byte[] codes = new UTF8Encoding(true).GetBytes(str);// Encoding process initialization UTF8Encoding New instance of class
//@ This can be thought of as a storage mode in a stream
fs.Write(codes, 0, codes.Length);// Write Stream
}
// The process of reading streams is 1 The process of decoding, that is, reading a sequence of bytes from a stream and restoring them to unicode character
public static string ReadStream(FileStream fs)
{
StringBuilder str = new StringBuilder();
byte[] b = new byte[fs.Length];// Establish 1 Sequence of bytes that can hold streams
UTF8Encoding utf = new UTF8Encoding(true);// Establish 1 individual UTF8Encoding Instance, specifying tag order
while (fs.Read(b, 0, b.Length) > 0)// Loop the bytes in the read stream into the specified byte sequence
{
str.Append(utf.GetString(b));// Decoding process
}
fs.Close();
return str.ToString();
}
}
StreamReader and StreamWriter
static void Main(string[] args)
{
////=================StreamReader Creation =============================
//// When reading or writing from a text file , First create 1 Associated with files StreamWriter or StreamReader object
//// and 1 Specifies the type of character encoding that is specified by default UTF8Encoding .
//// Create by: You can contact 1 Creation of the following file stream
////1.StreamReader Constructor directly created
//StreamReader sr1 = new StreamReader(@"f:\1.txt", UTF32Encoding.ASCII);
////2. utilize File Class or FileInfo Class OpenText Method Creation
//StreamReader sr2 = File.OpenText(@"f:\1.txt");// This method can only be coded as UTF8Encoding
////3. Now that you want to read the text stream, you can read it directly from FileStream So you can build on it.
//// Can also be used indirectly File Class or FileInfo Class-related method creation
//FileStream f1 = new FileStream(@"f:\1.txt",FileMode.Open,FileAccess.ReadWrite);
//StreamReader sr3 = new StreamReader(f1);
//// It can be inferred that, StreamReader Level can accept file path creation or file stream creation directly
////=================WriteReader Establish =============================
////1. Self-constructor created directly
//StreamWriter sw1 = new StreamWriter(@"f:\1.txt");
////2. utilize File Class or FileInfo Class CreateText and AppendText Method Creation ,// This method can only be coded as UTF8Encoding
//StreamWriter sw2 = File.AppendText(@"f:\1.txt");
//sw2 = new FileInfo(@"f:\1.txt").CreateText();
////3. Can be directly from FileStream So you can build on it.
//// Can also be used indirectly File Class or FileInfo Class-related method creation
//StreamWriter sw3 = new StreamWriter(f1);
//FileStream f2 = new FileInfo(@"f:\1.txt").Open(FileMode.Open);
//sw3 = new StreamWriter(f2);
//=================== Test Read and Write ====================
StreamWriter sw = WriteText();
StreamReader sr=new StreamReader(@"f:\1.txt");
string s = ReadText(sr);
Console.WriteLine(s);
Console.ReadKey();
}
// Read Method
public static string ReadText(StreamReader sr)
{
StringBuilder sb = new StringBuilder();
while (!sr.EndOfStream)
{
sb.AppendLine(sr.ReadLine());
}
sr.Close();
return sb.ToString();
}
// Writing Method
public static StreamWriter WriteText()
{
using (StreamWriter sw = new StreamWriter(@"f:\1.txt",true))
{
sw.WriteLine(DateTime.Now.ToString());
return sw;
}
}