C # How to read files

Source: Internet
Author: User

Key knowledge points:

1. filestream file_read = new filestream ("1.txt", filemode. Open, fileaccess. Read); // only the read permission is allowed to open the 1.txt file.

Parameter 1:

"1.txt": file path, usually represented by string variables or string constants, for example," d: \ 1.txt ";

Parameter 2:

Filemode. Open: open mode. If this mode exists, it is opened; otherwise, an exception is thrown.

Filemode. append: append mode. You can only use the fileaccess. Write Permission to open a file or create a new file and find the end of the file.

Filemode. Create: Specifies the file created by the operating system. If the file with the same name already exists, it is overwritten.

Filemode. createnew: Specifies the file created by the operating system. If the file with the same name already exists, an exception is thrown.

Filemode. openorcreate: if the file exists, open it. Create a new file if it does not exist.

Filemode. truncate: open an existing file. Once opened, the file is truncated to 0 bytes.

Parameter 3:

Fileaccess. Read: Read-only write permission

Fileaccess. readwrite: read/write permission

Fileaccess. Write: Write Permission

Ii. streamreader sr = new streamreader (file_read, encoding. Default); // get read/stream read/1.txt

Parameter 1:

File_read: get the file stream, that is, get the specified file stream, which is usually represented by a string variable.

Parameter 2:

Encoding. Default: obtains the encoding of the current ANSI code page of the operating system.

Encoding. utf8: Get the UTF-8 encoding.

Encoding. Unicode: Get the encoding in unocode format

Encoding. ASCII: Get the encoding in ascll format

Encoding. Convert (encoding, encoding, byte []): converts one encoding method to another.

Encoding. Convert (encoding, encoding, byte [], int32, int32): converts a range from one encoding method to another encoding method.

3. String [] filelist = file. readalllines ("1.txt", encoding. Default); // read all the lines in the file and save them to the string array.

4. String line = Sr. Readline (); // read the content of a file row and obtain the content from the read stream.

Example 1:

Core Method: String [] filelist = file. readalllines ("1.txt", encoding. Default); // read all the lines in the file and save them to the string array.

The Code is as follows:

Using system; using system. collections. generic; using system. text; using system. io; namespace studyread {class program {static void main (string [] ARGs) {filestream file_read = new filestream ("1.txt", filemode. open, fileaccess. read); // create a new file stream. Only the read permission is allowed to open the 1.txt file string [] filelist = file. readalllines ("1.txt", encoding. default); // read all the lines in the file content and save them to the string array. // Print the read content cyclically. For (INT I = 0; I <= filelist. length-1; I ++) {console. writeline ("Row {0} content: {1}", I, filelist [I]);} console. readkey ();}}}

Run:

Example 2:

Core Statement: String line = Sr. Readline (); // read the content of a file row and obtain the content from the read stream.

The Code is as follows:

Using system; using system. collections. generic; using system. text; using system. io; namespace studyread {class program {static void main (string [] ARGs) {filestream file_read = new filestream ("1.txt", filemode. open, fileaccess. read); // create a new file stream. Only the read permission is limited to open the 1.txt file streamreader sr = new streamreader (file_read, encoding. default); // create a new read-in-stream file stream string line = sr. readline (); // read a row of memory and save it to the string line console. writeline (line); console. readkey ();}}}

Run:

Of course, the code in the above column can also read all the content through loops to achieve the effect of Example 1.

Modify the source code as follows:

Using system; using system. collections. generic; using system. text; using system. io; namespace studyread {class program {static void main (string [] ARGs) {filestream file_read = new filestream ("1.txt", filemode. open, fileaccess. read); // create a new file stream. Only the read permission is limited to open the 1.txt file streamreader sr = new streamreader (file_read, encoding. default); // create a new read-in stream to fetch the file stream string [] filelist = file. readalllines ("1.txt", encoding. ASCII); // It is mainly used to obtain Total number of rows filelist. length string line; // defines the string variable line, used to save the read content // read and print each row of the file cyclically, that is, for (INT I = 0; I <= filelist. length-1; I ++) {line = sr. readline (); // read a row and save it to the string line. Console. writeline (line) ;}console. readkey ();}}}

Program:

Content of the appendix notepad file:

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.