1文件的读取和写入
1.1读取文件
方法 |
说明 |
File.ReadAllText(FilePath); |
读取指定路径的文件 |
File.ReadAllText(FilePath, Encoding); |
通过指定编码格式来读取指定文件 |
File.ReadAllBytes(); |
读取二进制文件,并把内容读取到一个字节数组 |
File.ReadAllLines(); |
以行的形式读取文件,一行一个字符串,返回一个字符串的数组 |
1.2写入文件
方法 |
描述 |
File.WriteAllText(); |
以字符串方式写入 |
File.WriteAllLines(); |
以字符串数组凡是写入 |
File.WriteAllBytes(); |
以字节数组方式写入 |
2.文本读取和写入流
2.1文本流的创建
2.1.1直接创建
StreamReader reader = new StreamReader(@"E:\file.zip");
2.1.2通过 FileStream 创建
FileStream fs = new FileStream(@"E:\file.zip");
StreamReader reader = new StreamReader(fs);
2.1.3通过 FileInfo 创建
FileInfo myFile = new FileInfo(@"E:\file.zip");
myFile.