C # Operations on files (Basic read-write and compression and decompression)

Source: Internet
Author: User

Mainly for a single file for read and write operations and compression operations: The main C # classes used are FileStream, FileInfo, Streamwrite, Streamread, GZipStream.

Conversion of character arrays and byte arrays:

 1 byte[] bytedata = new byte[200]; 2 char[] Chardata = new char[200];                 3 Try 4 {5 FileStream fs = new FileStream ("app. config", FileMode.Open); 6 Fs. Seek (0, Seekorigin.begin); 7 fs. Read (bytedata, 0, 200); 8} 9 catch (IOException io) ten {Console.WriteLine (IO). ToString ()); Console.readkey (); return;14}15 Decoder dc = Enc Oding. UTF8. Getdecoder ();//Create a decoder used to decode a binary array into a character array of DC. GetChars (bytedata, 0, Bytedata. Length, Chardata, 0); Console.WriteLine (Chardata); Console.readkey (); byte[] Byt Edata;20 char[] chardata;21 try22 {FileStream fs = new FileStream (" Log.txt ", filemode.create); chardata =" This is the user first log the software ". ToCharArray (); Encoder E = Encoding.UTF8.GetEncoder (); bytedata = new byte[chardata.length];27 e.getbytes (char Data, 0, Chardata.length, bytedata, 0, True), FS. Seek (0, Seekorigin.end); fs.                 Write (bytedata, 0, Bytedata.length),}31 catch (IOException io) 32 {33 Console.WriteLine (IO. ToString ()); Console.readkey (); return;36}37 Console.readkey ( );

Compress and decompress files (an example of a single file):

 1 string fileName = "CompressedFile.txt"; 2 Console.WriteLine ("Please input a word and it'll repeate"); 3 String inputstring = Console.ReadLine (); 4 StringBuilder sourcestring = new StringBuilder (inputstring.length * 100);             5 for (int i = 0; i < i++) 6 {7 sourcestring.appendline (inputstring); 8 } 9 String sourcecompresses = Sourcestring.tostring (); Console.WriteLine ("Source Dat  A ' s length is {0} ", sourcecompresses.length); try12 {compressedfile (FileName, sourcecompresses); Console.WriteLine ("Compressed successfully"); FileInfo FileInfo =                 New FileInfo (FileName); Console.WriteLine ("Compressed file ' s length is{0}", fileinfo.length); 17  String loadcompressed = Loadcompressedfile (FileName); Console.WriteLine (loadcompressed); 19           }20 catch (IOException io) {Console.WriteLine (IO). ToString ()); Console.readkey (); return;25}26 Console.readkey (             );}28 private static void Compressedfile (String fileName, string sourcecompress) 30 {31 FileStream fs = new FileStream (FileName, FileMode.Create, FileAccess.Write); GZipStream gzcompressed File = new GZipStream (FS, compressionmode.compress), StreamWriter sw = new StreamWriter (gzcompressedfile,enc Oding. UTF8); SW. Write (sourcecompress); Close ();}37 loadcompressedfile-Private static string (string FileName) eSTREAM fs = new FileStream (FileName, FileMode.Open, FileAccess.Read); GZipStream gzloadcompressed = new GZi PStream (FS, compressionmode.decompress), StreamReader sr = new StreamReader (GzLOADCOMPRESSED,ENCODING.UTF8); StringBuilder strbuild = new StringBuilder (); = Sr. ReadLine (); (!string.  IsNullOrEmpty (Strreadline)) (Strreadline strbuild.append (strreadline); = Sr. ReadLine ();}50 return strbuild.tostring (); 51}

C # Operations on files (Basic read-write and compression and decompression)

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.