Reading and writing of C # file streams

Source: Internet
Author: User

Cross-platform transfer of files encountered in the work the file stream is used and the files on the computer are read into the program memory and the data in the memory is saved in the form of files is more commonly used here to do a simple application example.

1 "Write" The memory data into the computer's file

Const string Path = @ "D:\text.txt";//path to save file
String content = "File contents!!!" ";//define the data in memory
byte[] bytes = Encoding.Default.GetBytes (content);//will be converted to binary data encoding format Encoding.default
int length = bytes. length;//If you want to write the data to the full length of the data to be written
using (FileStream stream = new FileStream (path, FileMode.Create))
{
Stream. Write (bytes, 0, length);//write
}

2 "read" The data in the computer files into memory

Const string Path = @ "D:\text.txt";//file to read data, example of a file that was previously written
Byte[] bytes;//object to create read results
using (FileStream stream = new FileStream (path, FileMode.Open))
{
bytes = new Byte[stream. length];//can determine the length of the read result
Stream. Read (bytes, 0, bytes. LENGTH);//Read operation
}
string result = Encoding.Default.GetString (bytes);//The result of the Read conversion type encoding format is best consistent with the encoding format written to prevent garbled
Console.Write (result);

3 Use the "read" and "write" operations of the file stream to implement a copy-like feature to print the copy progress

String rppath = @ "D:\ The song of Ice and Fire: the Game of Thrones." Game.of.thrones.2016.s06e03.hd720p.x264.aac.chs-eng.dytt.mp4 ";//I have such a video on the C drive of my Computer
String wPath2 = @ "E:\A.mp4";//I want to write it to E-drive
Byte[] bytes;//defines a container binary type that needs to be written and read
using (FileStream Rstream = new FileStream (Rppath, FileMode.Open))
{
bytes = new byte[rstream.length];//all read
Rstream.read (bytes, 0, bytes. Length);
}
using (FileStream Wstream = new FileStream (wPath2, FileMode.Create))
{
int num = bytes. Length/(1024 * 1024);//the size of each read is 1MB
for (int i = 0; I <= num; i++)
{
Wstream.write (bytes, 0, num);
i++;
Console.Write (Math.Round ((I/(float) num), 2);//print Read%
Console.Write ("\ n");
}
}

Reading and writing of C # file streams

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.