Dark Horse programmer: File Operations

Source: Internet
Author: User

---------------------- ASP. NET + Android + I/O Development S,. Net training, hope to communicate with you! ----------------------

File Operations

File. copy ("Source", "targetfilename", true); // copy a file. "True" indicates "Overwrite" when the file exists. If "true" is not added, an exception is thrown to the file. File. exists (); // determine whether the file exists. Move ("Source", "target"); // move (CUT) and think about how to rename the file? Files can be cut across disks. File. Delete ("path"); // Delete. If the file does not exist? Does not exist. file. Create ("path") is not reported; // Create File file. readalllines ("path", encoding. Default); // read all rows and return string []
File. readalltext ("path", encoding. Default); // read all text and return string
File. readallbytes ("path"); // read the file, return byte [], and process the file as binary.
L ===================================================== ====
File. writealllines ("path", new string [4], encoding. Default); // write the string array to a file by row.
File. writealltext ("path", "string"); // write all strings to the file
File. writeallbytes ("path", newbyte [5]); // write all byte [] to the file
File. appendalltext () // append string to the file stream operation filestream FS = file. open (); // return filestream
Filestream FS = file. openread (); // returns the read-only filestream.
Filestream FS = file. openwrite (); // return the write-only filestream.
Filestream FS = new filestream (parameter );

Stream (the parent class of all streams. It is an abstract class .)
All file operation classes are in the system. io. * Two Methods for copying files: read all the source files to the memory and write them to the target file. Read the 1 kb memory of the source file and write it to the target file, read the 1 kb memory of the source file and write it to the target file ....... The second method is a stream operation. L using file. readalltext and file. writealltext for file read and write is a one-time read and write. If the file is very memory-consuming and slow. The mechanism for reading a row to process a row is stream ). Stream only reads the required position and length. Stream does not read all the content to the memory at a time. It has a pointer pointing to where it can be read and written. There are many types of streams, one of which is file streams. The optional values and meanings of the last two parameters of the filestream class newfilestream ("C:/a.txt", filemode, and fileaccess) can be viewed by yourself. Filestream is readable and writable. You can use file. openread and file. openwrite to simplify the call methods. Byte [] is the most fundamental representation of any data, and any data is eventually binary. The position attribute of filestream is the pointer position of the current file. Every write time, you need to move the position for the next write. Write is used to write several bytes to the current location, and read several bytes. Use filestream to write text files.
Use filestream to write and read text files. What is the effect of Reading 1 byte each time for Chinese characters? Better choice → streamreader
Use filestream to read text files.
Use filestream to copy large files.
File. readallbytes () and file. writeallbytes () are copied. (View memory usage)
You can use using to easily release resources (call the dispose method automatically)
Only classes that implement the idispose interface can use using to release resources.

Stream treats all the content as binary. If it is text content, the programmer must handle the conversion between the text and binary.
Streamwriter can simplify text stream processing.
Streamwriter is used to assist stream processing.
Using (streamwriter writer = new streamwriter (stream, encoding ))
{
Writer. writeline ("hello ");
}

Similar to streamwriter, streamreader simplifies the reading of text streams.
Stream stream = file. openread ("C:/1.txt ");
Using (streamreader reader = new streamreader (stream, encoding ))
{
// Console. writeline (reader. readtoend ());
Console. writeline (reader. Readline ());
}
Readtoend is used to read from the current position until the end. If the content is large, it will occupy the memory. Each call goes down and cannot be called twice accidentally.
Readline reads a row. If it reaches the end, null is returned.

Cause of Garbled text (only text files are garbled): The encoding used for text file storage is inconsistent with the encoding used for reading, which may cause Garbled text. Solution: Use unified encoding. text file encoding, text files have different storage methods, the string in what form to save as binary, this is encoding, UTF-8, ASCII, Unicode, etc, if garbled characters occur, they are generally encoded. functions related to text files generally have an encoding parameter. The encoding method is encoding. default, encoding. utf8, encoding. getencoding ("GBK") file encoding (code table) • ASCII: English code table, each character occupies 1 byte. • Gb2312: compatible with ASCII and contains Chinese characters. Each English occupies one byte (positive), and Chinese occupies two bytes (negative) • GBK: Simplified Chinese, compatible with gb2312, including more Chinese characters. English occupies 1 byte (positive), Chinese occupies 2 (1 negative, 1 positive and negative) • big5: Traditional Chinese • UNICODE: international code table, both Chinese and English are supported in 2 bytes. • UTF-8: international code table. English occupies 1 byte and Chinese occupies 3 byte. L output encoding. getencodings (), all encoding. What is a text file. Drag it to notepad to see the text file, Doc is not. Common static methods of the file class: (fileinfo *) • voidappendalltext (string path, string contents), attaches the text contents to the file path • bool exists (string path) determine whether the file path exists • string [] readalllines (string path) reads the text file to a string array • stringreadalltext (string path) reads the text file to a string • voidwritealltext (string path, string contents) saves text contents to the file path, and overwrites the old content. • Writealllines (string path, string [] Contents) saves the string array row by row to the file path, and overwrites the old content.

Returns the inverse of the binary value corresponding to a positive number, and Adds 1 to the binary value of a negative number.
Encoding. Default

System. Text. dbcscodepageencoding dubyte character set. If encodingname is output, the result is: Simplified Chinese (gb2312)

Encoding. Default:It is related to the current "region and language options" of the operating system.

//// If the system does not provide encoding, you can get the encoding through getencoding ("encoded amount string representation.


// Encodingencoding = encoding. getencoding ("gb2312 ");


StringbuilderSB =NewStringbuilder();


Encodinginfo[] Infos = system. Text.Encoding. getencodings ();


For(IntI
= 0; I <Infos. length; I ++)

{

SB. append (Infos [I]. codePage +""+
Infos [I]. displayname +
""+ Infos [I]. Name +"\ R \ n");

}


File. writealltext ("encodings.txt", SB. tostring ());

This is the File Operations Section. For more information, see the next article.

-------------------- ASP. NET + Android + iOS development,. Net training, and hope to communicate with you! ----------------------

See http://edu.csdn.net for details

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.