File read/write and Data Processing

Source: Internet
Author: User
Tags binary to decimal

Happy New Year ;)

This isCodeThe example can be run directly, mainly including the processing of binary data streams and character data during file read/write.
In addition, I have written detailed comments for you to learn more conveniently.
:

Using system; using system. net. networkinformation; using system. text; using system. io; namespace consoleapplication1 {class program {static void main (string [] ARGs) {binarywriteread (); // copy (); // console. writeline ("copied"); // txtread ();} // binary read/write Integrated Application (suitable for games such as data storage) // pay attention to static void binarywriteread () such as the memory change bytes in this instance {// write to the disk (in the memory, it is output because it is output to external devices) // In the operating system, stream is a filestream FS = file with memory as the reference. create ("Save"); system. io. binarywriter writer = new binarywriter (FS); random RDM = new random (); For (INT I = 0; I <10; I ++) // int32 4-byte x10 {int num = RDM. next (); writer. write (Num); console. writeline ("Write INTEGER: {0}", num);} Char [] DATA = {'h', 'E', 'l', 'l ', 'o'}; writer. write (data); console. writeline ("Write: Hello"); writer. write ('-'); // 46 bytes // write the ASCII code of 26 letters // If streamreader is used, the corresponding ASCII letter for (byte I = 97; I <97 + 26; I ++) writer. Write (I); console. writeline ("Write ~ Z "); writer. write ('\ n'); // 71-byte console. writeline ("----------------------------------------"); FS. flush (); FS. close (); writer. close (); // ------------------------------------------------------------- // read (input in memory) FS = file. openread ("save"); binaryreader reader = new binaryreader (FS); // read 26 letters FS. seek (46, seekorigin. begin); For (byte I = 97; I <97 + 26; I ++) console. write (reader. readchar (); // read Take 10 integers FS. seek (-72, seekorigin. current); console. writeline (); // int indicates system. int32 4 bytes // If writer. write (true) is actually written into one byte: 0x0001 // byte is the basic unit byte [] numdata = new byte [4]; for (INT I = 0; I <10; I ++) {reader. read (numdata, 0, 4); console. writeline ("read INTEGER: {0}", byteartolong (numdata); // manually convert the byte array // console. writeline ("read INTEGER: {0}", reader. readint32 ();} reader. close (); FS. close ();} static int getnu M (byte [] bytes) // convert binary to decimal (in my post, leave a comment on the brother's code which is awesome x ^_^) {// The long data type is 8 bytes int num = 0; int n = math. min (bytes. length, 4)-1; for (INT I = N; I>-1; I --) num = (Num <8) | bytes [I]; // num = (Num <8) + bytes [I]; return num;} // convert the byte array to a long integer (Binary to decimal) static long byteartolong (byte [] bytes) {// int [] Nums = {1,256,512,102, 2048, 4096,8192, 16384,32768, 65536,131072, 262144 }; // At that time, my code was written in this way. A big number may cause problems. The 8-byte value should be 8 square characters while I Write is indeed 2 square // but the above-mentioned Message expert only cares about his own happiness, such an obvious error is not seen // int srcdata = 312705998; // For example, the int value above is stored in the form of 0x12a383ce in the memory // but it is stored in the file in reverse (the low position is the top position and the back position is the top position) // if I read 4 bytes at a time, it is in the following format // byte [] DATA = {0xce, 0x83, 0xa3, 0x12 }; int [] Nums = {1,256,655 36, 16777216}; If (bytes. length> nums. length) throw new exception ("overflow"); long num = 0; For (INT I = 0; I <bytes. length; I ++) num + = (Bytes [I] * Nums [I]); Return num;} static void Copy () // file copy (Binary read/write) {filestream FS = file. openread ("D:/create a folder (2).zip"); fileinfo copys = new fileinfo ("D:/Appendix .zip"); filestream fscopy = copys. create (); byte [] DATA = new byte [10]; int Reds; while (Reds = FS. read (data, 0, Data. length)> 0) fscopy. write (data, 0, Reds); FS. flush (); fscopy. close (); FS. close ();} static void txtread () // text reading {// ANSI encoding is a tough thing. // For example, UTF-8 and other encoded text files will start with two bytes to recognize the encoding/ /While ANSI is not, but ANSI is expanded to include Chinese character encoding. // I tried to read binary and decode it before, but it is really troublesome. // still like this easy to be lazy) O Haha ~ Filestream FS = file. open (@ "D:/. CS ", filemode. open); streamreader reader = new streamreader (FS); string data; while (Data = reader. readline ())! = NULL) console. writeline (data );}}}

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.