The first example of file read/write, the first example of read/write
First, open VS2013 and create a form application.
Requirement: to implement this function, click the text box and the file dialog box will pop up. Let's select a file and click OK to read the content of the specified file, then we will write the read data into the text.
Principle of file read/write: Previously, when learning the principle of file read/write, we knew the relationship between hard disk data and memory. On the disk, we read data into binary data, store it in the byte array, and then in the memory, we need to convert the byte array into the original text data.
Then read the code:
We use the OpenFileDialog class to create a dialog box. The ShowDialog method of the instance object is used to display the dialog box. The return value of this method is DialogResult type. The code in the figure indicates that when we open the dialog box and click OK, the full path of the selected file is displayed.
The code is written in this way, and the same effect can be achieved through filenames: see figure:
Now we have achieved this effect, showing the file path. Now we need to click the read button to read the selected file and data to another file.
The text we tested is:
We can see that the text is 293 Bytes: (in the code, how do we know the size of the created array ??)
Here is a tip:
First, read the path of the passed text. The length attribute in the file stream indicates the size of the file to be read. We can know the size of the array to be created.
All code:
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. IO; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace FileReadDemo {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void txtPath_Click (object sender, EventArgs e) {// open file dialog box OpenFileDia Log openDialog = new OpenFileDialog (); // display the dialog box. The returned value is DialogResult type // openDialog. showDialog (); if (openDialog. showDialog () = DialogResult. OK) {txtPath. text = openDialog. fileName; // get full path} private void btnRead_Click (object sender, EventArgs e) {// 1. to read a file, we need to use the file read/write class. Here we use the FileStream class to reference the command space System. IO. // read the file. Select Open FileStream fileRead = new FileStream (txtPath. text, FileMode. open); // create an array of the byte type. (Note that the size of the array should be large ?) Byte [] fileByte = new byte [fileRead. length]; // 2. read the specified data in the file to the array fileRead. read (fileByte, 0, fileByte. length); // according to the file read/write principle, the data on the disk is converted to binary and saved to the array. Now you need to write data to the file. // The FileStream class can only do one thing, either read the file or write the file. Solution: You can create another FileStream // note. No matter whether the file stream is released. Test phase, which will be optimized later. // To facilitate the test, you can choose to read the file to the aaa.txt file on the ddrive. // Write the file, select Create FileStream fileWrite = new FileStream (@ "D: \ aaa.txt", FileMode. create); // then, fileWrite is written into the file. write (fileByte, 0, fileByte. length); // test code. After the code is completed, OK MessageBox is displayed. show ("OK ");}}}
Note: In this example, you need to stop the program after the execution is successful to see the data read from the file, and the function of FileStream caching ..