Io stream-related 01

Source: Internet
Author: User

Common static methods of the file class:

  1. Void appendalltext (string path, string contents), attaches the text contents to the file path
  2. Bool exists (string path) determines whether the file path exists
  3. String readalltext (string path) reads text files to strings
  4. String [] readalllines (string path) read text files to the string array
  5. Void writealltext (string path, string contents) saves the string contents to the file path and overwrites the old content.
  6. Writealllines (string path, string [] Contents), save the string array row by row to the file path, and overwrite the old content.

 

Common static methods of directory classes:

  1. Delete (string path, bool recursive) deletes the directory. Recursive indicates whether to recursively Delete the directory. If recursive is false, only empty directories can be deleted.
  2. Bool exists (string path) determines whether the directory exists
  3. Createdirectory: Create a folder
  4. String [] getdirectories (string path) to get a subdirectory under the Directory
  5. String [] getdirectories (string path, string searchpattern, searchoption)
  6. Static string [] getfiles (string path)
  7. String [] getfiles (string path, string searchpattern, searchoption)
  8. Directoryinfo getparent (string path) to get the parent directory of the Directory
Listall (@ "F: \"); ------------- execution method !! /// <Summary> /// indicates all. jpg files in a directory. // </Summary> /// <Param name = "dir"> </param> static void listall (string DIR) {// obtain all files in the directory string [] files = directory. getfiles (DIR); foreach (string file in files) {If (file. endswith (". jpg ") {console. writeline (File) ;}/// obtain the names of all subdirectories in the directory string [] dirs = directory. getdirectories (DIR); foreach (string D in dirs) {// recursively call the listall method listall (d );}}

 

Common static methods of the path class:

String changeextension (string path, string extension) (*) modifies the file suffix. "modification" supports string-level modification. The file is not renamed string S = path. changeextension (@ "C: \ temp \ f3.png", "jpg ")

String combine (string path1, string path2) combines two paths into one path, which is better than +, and can solve the problem of adding a diagonal line, automatic Path Separator Processing string S = path. combine (@ "C: \ Temp", "a.jpg ")

String getdirectoryname (string path) (*) to obtain the path name of the object. Path. getdirectoryname (@ "C: \ temp \ a.jpg") ----- return c: \ Temp

String getextension (string path) to get the file extension

String getfilename (string path) to get the file name part of the file path

String getfilenamewithoutextension (string path) Get the file name with the removed Extension

String getfullpath (string path) to obtain the full path of the file .". \ "Current path,"... \ "upper-level path,"... \ "upper-level path

String gettempfilename () to get a unique temporary file name (*)

String gettemppath () to obtain the path of the Temporary Folder (*)

Obtain the path of the current exe. Assembly. getexecutingassembly (). Do not use directory. getcurrentdirectory (). This may change (use openfiledialog or setcurrentdirectory ()). (*) Solve the access file path problem.

1 // obtain the path of the currently executed EXE 2 string location = assembly. getexecutingassembly (). location; 3 4 // get the folder directory where the current EXE is located --- go out of the path *. EXE 5 string locdir = path. getdirectoryname (location); 6 7 // merge two path strings, 8 string newfilename = path. combine (locdir, "1.txt"); 9 10 // file. readalltext reads all rows of the specified file and returns string type 11 // file. readalllines returns string [] array 12 console. writeline (file. readalltext (newfilename, encoding. default ));

 

Common static filestream methods:

  1. Using file. readalltext and file. writealltext to read and write files is a one-time read and write operation. If a file is very memory-consuming, it is slow.
  2. The mechanism for reading a row to process a row is stream ). Stream only reads the required position and length.
  3. Using (filestream inputstream = new filestream (textbox1.text, filemode. Open) ---- filemode. Open
  4. Using (filestream outputstream = new filestream (textbox2.text, filemode. Create) ---- filemode. Create indicates the output stream
  5. Byte [] is the most fundamental representation of any data, and any data is eventually binary.
  6. Readbytes = inputstream. Read (bytes, 0, bytes. Length) ---- reads bytes from the stream and writes the data to the given buffer (bytes ).

Exercise: file encryption (255-r for each bit)

1 using system; 2 using system. collections. generic; 3 using system. componentmodel; 4 using system. data; 5 using system. drawing; 6 using system. io; 7 using system. text; 8 using system. windows. forms; 9 10 namespace filestream encryption 11 {12 public partial class form1: form13 {14 public form1 () 15 {16 initializecomponent (); 17} 18 19 private void button#click (Object sender, eventargs e) 20 {21 // Initialize an openfiledialog Class 22 openfiledialog filedialog = new openfiledialog (); 23 24 // determine whether the user has correctly selected the file 25 if (filedialog. showdialog () = dialogresult. OK) 26 {27 // Save the path selected by the user in the text box 28 textbox1.text = filedialog. filename; 29 30} 31} 32 33 private void button2_click (Object sender, eventargs E) 34 {35 36 37 // instantiate an instance of reading a file stream ---- open38 using (filestream inputstream = new filestream (textbox1.text, filemode. open) 39 {40 // progress bar code 41 // obtain the size of the target file. fileinfo indicates the size of the file to be viewed by the system (equivalent to right-click and view attributes ), instead of reading the memory to view the size of 42 fileinfo = new fileinfo (textbox1.text); 43 44 // fileinfo. length returns the size of the current file, which is of the long type. It must be converted to the int type. 45 // progressbar1.maximum indicates the maximum value of the progress bar. 46 progressbar1.maximum = (INT) fileinfo. length; 47 48 // instantiate an instance that writes a file stream ---- create49 using (filestream outputstream = new filestream (textbox2.text, filemode. create) 50 {51 // buffer, that is, the size of one read/write is 52 byte [] bytes = new byte [1024*1024*4]; 53 // variable 54 int readbytes of the current read size; 55 56 // It indicates reading the byte block from the stream and writing the data into the given buffer (bytes) starting from the offset of 0 at the current position, that is, reading the maximum bytes from the current position. length is so large 59 // readbytes = inputstream. read (bytes, 0, bytes. length is a value assignment expression, and its value is equal to the final value 60 // For example: int A = 1 ++; the value of the value assignment expression is equal to 261 while (readbytes = inputstream. read (bytes, 0, bytes. length)> 0) 62 {63 // encrypt the read data 64 // bytes is the specified buffer size, that is, the maximum number of reads 65 // readbytes is the actual read size 66 for (INT I = 0; I <readbytes; I ++) 67 {68 // use byte for each byte. maxvalue --- 255 minus, and then save 69 bytes [I] = (byte) (byte. maxvalue-bytes [I]); 70} 71 // The output stream reads data from the buffer and writes the data to the stream 72 outputstream. write (bytes, 0, readbytes); 73 74 // the current value of the progress bar is 75 progressbar1.value = progressbar1.value + readbytes; 76} 77 // indicates that the write is completed 78 If (progressbar1.value = progressbar1.maximum) 79 {80 MessageBox. show ("OK"); 81} 82} 83} 84} 85} 86}

 

Related Article

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.