Simple file stream writing and reading

Source: Internet
Author: User

# 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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.