File reading and writing methods in the C# File class

C#Provides a variety of file manipulation schemes, Filestatic methods encapsulated in the class, and the interface is encapsulated more humanely, hiding the details of the specific implementation, mainly including reading, writing, and appending. These functions are as follows

category Byte read and write list of strings string
read ReadAllBytes ReadAllLines ReadAllText ReadLines
to write WriteAllBytes WriteAllLines WriteAllText
addition AppendAllLines AppendAllText AppendText

Most of these methods have their asynchronous modes. For RaadAllBytesexample, the corresponding asynchronous method is ReadAllBytesAsync, that is, add one at the end Async.

Among these methods, the read operation needs to input at least one string, indicating the file path, and its return value is the number of bytes, a string or a list of strings; the append and write operations need to input at least one file path and the value to be written. Contents, depending on the function class, can be a byte array, a string, or a list of strings.

Let's do some basic demonstrations of these functions in the top-level statement.

Byte read and write

ReadAllBytesand WriteAllBytesare methods for reading and writing byte arrays,

byte[] data = new byte[1024];
for (int i = 0; i < 1024; i++)
    data[i] = (byte)i;

File.WriteAllBytes("test.bin", data);

After running, a test.binfile will be created in the running directory, and then ReadAllBytesread by it.

data = File.ReadAllBytes("test.bin");
foreach (var d in data)
    Console.Write($"{
      
      d}, ");

The output result is a number from 0-255, similar to the following, which has been converted into bytes when the file is created, i1 byte has only 8 bits, and the maximum value is 255.

...251, 252, 253, 254, 255, 0, 1, 2, 3, 4, 5...

String reading and writing and appending

By xxxAllTextbeing able to read text documents directly, these functions provide two overloads, one only needs to input the file name and write content, and the other needs to specify the encoding method.

File.WriteAllText("test.txt", "if u miss the train i'm on, ");
File.AppendAllText("test.txt", "u will know that i'm gone");
var s = File.ReadAllText("test.txt");
Console.WriteLine(s);

The output is

if u miss the train i'm on, u will know that i'm gone

If the encoding method is specified, although it may not matter to English, the encoding mismatch may cause garbled characters. The example is as follows

File.WriteAllText("test.txt", "桃李春风一杯酒");
File.AppendAllText("test.txt", "江湖夜雨十年灯", Encoding.Unicode);

var s = File.ReadAllText("test.txt");
Console.WriteLine(s);

The effect is

桃李春风一杯酒_lVnY?ASt^op

array of strings

xxxAllLinesArrays of strings, or other iterable objects with strings as elements, can be manipulated.

string[] poem0 = new string[] {
    
     "我居北海君南海", "寄雁传书谢不能" };
string[] poem1 = new string[] {
    
     "桃李春风一杯酒", "江湖夜雨十年灯" };
File.WriteAllLines("lines.txt", poem0);
File.AppendAllLines("lines.txt", poem1);

var s = File.ReadAllLines("lines.txt");
foreach (var item in s)
    Console.WriteLine(item);

The effect is as follows

我居北海君南海
寄雁传书谢不能
桃李春风一杯酒
江湖夜雨十年灯

Guess you like

Origin blog.csdn.net/m0_37816922/article/details/130667426