Read and Write the upload (.txt) file. net

Source: Internet
Author: User

(1) reading files

If you want to read not many files, you can use file. readalltext (filepath) or specify the encoding method file. readalltext (filepath, encoding. They read all the text content at a time and return a string containing all the text content.

 
StringSTR = file. readalltext (@ "C: \ temp \ ascii.txt");
// You can specify the encoding method.
StringStr2 = file. readalltext (@ "C: \ temp \ ascii.txt", Encoding. ASCII );

You can also use file. readalllines. This method returns a string array. Each row is an array element.

 
String[] STRs = file. readalllines (@ "C: \ temp \ ascii.txt");
// You can specify the encoding method.
String[] Strs2 = file. readalllines (@ "C: \ temp \ ascii.txt", Encoding. ASCII );

 

When the text content is large, we should not read the text content once, but use stream to read the content .. Net encapsulates the streamreader class for us. There are many methods to initialize the streamreader class. Below I will list several

Streamreader SR1 = New Streamreader ( @ "C: \ temp \ utf-8.txt" );
// You can also specify the encoding method.
Streamreader Sr2 = New Streamreader ( @ "C: \ temp \ utf-8.txt" , Encoding. utf8 );

Filestream FS = New Filestream ( @ "C: \ temp \ utf-8.txt" , Filemode. Open, fileaccess. Read, fileshare. None );
Streamreader sr3 = New Streamreader (FS );
Streamreader sr4 =New Streamreader (FS, encoding. utf8 );

Fileinfo myfile = New Fileinfo ( @ "C: \ temp \ utf-8.txt" );
// Create a UTF-8-encoded streamreader object in opentext
Streamreader sr5 = myfile. opentext ();

// Create a UTF-8-encoded streamreader object in opentext
Streamreader sr6 = file. opentext ( @ "C: \ temp \ utf-8.txt" );

After initialization, you can read a row or a character each time. You can also read several characters or even read all the content at a time.

 // Read a row 
String Nextline = Sr. Readline ();

// Read one character
Int Nextchar = Sr. Read ();

// Read 100 characters
Int Nchars = 100;
Char [] Chararray = New Char [Nchars];
Int Ncharsread = Sr. Read (chararray, 0, nchars );

// Read all
String Restofstream = Sr. readtoend ();

After streamreader is used, do not forget to close it: Sr. closee ();

If we need to read the entire text file in one row, let's take a complete example:

 
Streamreader sr = file. opentext (@ "C: \ temp \ ascii.txt");
StringNextline;
While(Nextline = Sr. Readline ())! =Null)
{
Console. writeline (nextline );
}
Sr. Close ();

(2) Writing files

Writing a file is the same as reading a file. If you want to write less content, you can use the file. writealltext method to write all the content at a time as a file. If you want to write the content of a string to a file, you can use file. writealltext (filepath) or specify the encoding method file. writealltext (filepath, encoding.

StringStr1 ="Good morning! ";
File. writealltext (@ "C: \ temp \ test \ ascii.txt", Str1 );
// You can specify the encoding method.
File. writealltext (@ "C: \ temp \ test \ ascii-2.txt", Str1, encoding. ASCII );

If you have a string array and want to write each string element into the file, you can use the file. writealllines method:

String[] STRs = {"Good morning! ","Good afternoon! "};
File. writealllines (@ "C: \ temp \ ascii.txt", STRs );
File. writealllines (@ "C: \ temp \ ascii-2.txt", STRs, encoding. ASCII );

When the file. writealltext or file. writealllines method is used, if the specified file path does not exist, a new file is created. If the file already exists, the original file is overwritten.

When you want to write a large amount of content, you also need to use stream writing .. The class encapsulated by. NET is streamwriter. There are also many methods to initialize the streamwriter class:

 // Create a file if the file does not exist. If yes, overwrite the file. 
Streamwriter SW1 = New Streamwriter ( @ "C: \ temp \ utf-8.txt" );
// You can specify the encoding method.
// True is append text, and false is used to overwrite the original file.
Streamwriter sw2 = New Streamwriter ( @ "C: \ temp \ utf-8.txt" , True , Encoding. utf8 );

// Filemode. createnew: Creates a file if the file does not exist. If the file already exists, an exception is thrown.
Filestream FS = New Filestream ( @ "C: \ temp \ utf-8.txt" , Filemode. createnew, fileaccess. Write, fileshare. Read );
// UTF-8 is the default encoding
Streamwriter sw3 = New Streamwriter (FS );
Streamwriter sw4 = New Streamwriter (FS, encoding. utf8 );

// Create a file if the file does not exist. If yes, overwrite the file.
Fileinfo myfile = New Fileinfo (@ "C: \ temp \ utf-8.txt" );
Streamwriter sw5 = myfile. createtext ();

After initialization, you can use a streamwriter object to write a row, a character, a character array, or even a part of a character array at a time.

// Write a character
Sw. Write ('A');

// Write an array of Characters
Char[] Chararray =New Char[100];
// Initialize these characters
Sw. Write (chararray );

// Write part of a character array
Sw. Write (chararray, 10, 15 );

Similarly, do not close streamwriter objects after they are used up. Sw. Close (); finally, let's take a complete example of using streamwriter to write a row at a time:

 
Fileinfo myfile =NewFileinfo (@ "C: \ temp \ utf-8.txt");
Streamwriter Sw = myfile. createtext ();

String[] STRs = {"Good morning","Good afternoon"};
Foreach(VAR sInSTRs)
{
Sw. writeline (s );
}
Sw. Close ();

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.