One, read the file
If the contents of the file you want to read are not many,
You can use File.readalltext (FilePath) or specify encoding methods File.readalltext (FilePath, Encoding).
They all read the text all at once and return a string containing all the text content
String str = File.readalltext (@ "C:\temp\ascii.txt");
You can also specify the encoding method
String str2 = File.readalltext (@ "C:\temp\ascii.txt", encoding.ascii);
You can also use the method File.ReadAllLines. The method returns an array of strings. Each row is an array element.
string[] STRs = File.ReadAllLines (@ "C:\temp\ascii.txt");
You can also specify the encoding method
string[] Strs2 = File.ReadAllLines (@ "C:\temp\ascii.txt", encoding.ascii);
When the content of the text is larger ,
Instead of reading the text content one at a time, we should use the stream to read the content. NET encapsulates the StreamReader class for us.
There are a number of ways to initialize the StreamReader class.
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");
OpenText creating a UTF-8-encoded StreamReader object
StreamReader SR5 = Myfile.opentext ();
OpenText creating a UTF-8-encoded StreamReader object
StreamReader SR6 = File.OpenText (@ "C:\temp\utf-8.txt");
After initialization, you can read one line at a time, read a single character at a time, read several characters at a time, or even read all the contents at once.
Read a line
String nextline = Sr. ReadLine ();
Read a 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 ();
When you're finished using StreamReader, don't forget to close it:
Sr. Closee ();
If we need to read the entire text file in one line, let's look at a complete example:
StreamReader sr = File.OpenText (@ "C:\temp\ascii.txt");
String nextline;
while (nextline = Sr. ReadLine ()) = null)
{
Console.WriteLine (nextline);
}
Sr. Close ();
Second, write the file
If you're not writing a lot of things ,
You can use the File.writealltext method to write the contents all at once as a file.
If you want to write the contents of a string to a file, you can use File.writealltext (FilePath) or specify the encoding method File.writealltext (FilePath, Encoding).
String str1 = "Good morning!";
File.writealltext (@ "C:\temp\test\ascii.txt", str1);
You can also specify the encoding method
File.writealltext (@ "C:\temp\test\ascii-2.txt", str1, Encoding.ascii);
If you have a string array and you want to write each string element to a file, you can use the File.writealllines method:
String[] STRs = {"Good morning!", "Good afternoon!"};
File.writealllines (@ "C:\temp\ascii.txt", STRs);
You can also specify the encoding method
File.writealllines (@ "C:\temp\ascii-2.txt", STRs, Encoding.ascii);
When using the File.writealltext or File.writealllines method, if the specified file path does not exist, a new file is created, and if the file already exists, the original file is overwritten.
when the content to be written is relatively long,
Also use the stream to write the same way: NET encapsulates the class is StreamWriter.
There are a number of ways to initialize the StreamWriter class:
//If the file does not exist, create the file, or overwrite the file if it exists
StreamWriter SW1 = new StreamWriter (@ "C:\temp\utf-8.txt");
//You can also specify the encoding method
//True is append text, False to overwrite the original file
StreamWriter SW2 = new StreamWriter (@ "C:\temp\utf-8.txt", true, Encoding.UTF8);
//filemode.createnew: Create file If file does not exist, throw exception if file already exists
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);
//If the file does not exist, create the file, or overwrite the file if it exists
FileInfo myFile = new FileInfo (@ "C:\temp\utf-8.txt");
StreamWriter sw5 = Myfile.createtext ();
After initialization is complete, you can write one line at a time with a StreamWriter object, a character, an array of characters, or even part of a character array.
Write a character
Sw. Write (' a ');
Write a character array
char[] Chararray = new char[100];
Initialize these characters
Sw. Write (Chararray);
Write part of a character array
Sw. Write (Chararray, 10, 15);
Again, do not forget to close the StreamWriter object when it is finished.
Sw. Close ();
Finally, consider a complete example of using StreamWriter to write one line at a time:
FileInfo myFile = new FileInfo (@ "C:\temp\utf-8.txt");
StreamWriter SW = Myfile.createtext ();
string[] STRs = {"Good Morning", "Nice Afternoon"};
foreach (var s in STRs)
{
SW. WriteLine (s);
}
SW. Close ();
C # Read and write files