# Region file stream writing
Private void WriteFileInfo ()
{
FileInfo myFile = new FileInfo (@ "c: \ tem \ aa.txt ");
FileStream stream = null;
// If (! MyFile. Exists ())
//{
// Stream = myFile. Create ();
//}
Stream = myFile. Open (FileMode. OpenOrCreate );
Byte [] byteArr = {0x00, 0x12, 0x34, 0x56, 0xAA, 0x55, 0xFF };
Stream. Write (byteArr, 0, byteArr. Length-1 );
Stream. Flush ();
Stream. Close ();
}
# Endregion
If the file system information is changed, call the Refresh () method.
There are many ways to create a file stream. We can also
Instantiate a file stream
FileStream stream = new FileStream (myfile, FileMode. Create );
# Region reading file streams
Private void ReadFileStream ()
{
FileStream fileStream = null;
Try
{
FileStream = new FileStream (@ "c: \ tem \ aa.txt", FileMode. Open );
Byte [] dataArry = new byte [fileStream. Length];
For (int I = 0; I <fileStream. Length; I ++)
{
DataArry [I] = (byte) fileStream. ReadByte ();
}
}
Finally
{
If (fileStream! = Null)
FileStream. Close ();
}
}
# Endregion
In itself, streams are not very useful because they work in the form of a single byte array ,. net has a more useful high-level model for reading and writing objects to bridge this gap. These objects encapsulate stream objects and allow you to write more complex data.
You can find the File class, FileStream class, FileInfo class, StreamWriter class, And StreamReader class in the. net Framework.
FileStream fileStream = new FileStream (@ "c: \ temp \ aa.txt", FileMode. Create );
StreamWriter w = new StreamWriter (fileStream );
Same as StreamWriter w = File. CreateText (@ "c: \ temp \ aa.txt ");
W. WriteLine ("what is this ");
W. WriteLine (1000 );
# Region reads data using StreamReader
Private void ReadStream ()
{
StreamReader r = File. OpenText (@ "c: \ myFile.txt ");
String line;
Do
{
Line = r. ReadLine ();
} While (line! = Null );
}
# Endregion
We use BinaryWriter and BinaryReader to read binary files. When reading data, you must know the data type to be obtained.