您好,登录后才能下订单哦!
在C#编程中,文件操作是非常常见的任务。无论是读取配置文件、处理日志文件,还是保存用户数据,文件操作都是不可或缺的一部分。C#提供了多种方式来读取和写入文件,本文将详细介绍这些方法。
File
类File
类是System.IO
命名空间中的一个静态类,提供了许多用于文件操作的静态方法。使用File
类可以方便地进行文件的读取和写入。
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
string content = File.ReadAllText(path);
Console.WriteLine(content);
}
}
File.ReadAllText
方法会将整个文件内容读取为一个字符串。如果文件较大,可能会占用较多内存。
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
string content = "Hello, World!";
File.WriteAllText(path, content);
}
}
File.WriteAllText
方法会将字符串内容写入到指定文件中。如果文件已存在,则会覆盖原有内容。
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
string content = "Hello, World!";
File.AppendAllText(path, content);
}
}
File.AppendAllText
方法会将字符串内容追加到指定文件的末尾,而不会覆盖原有内容。
StreamReader
和StreamWriter
StreamReader
和StreamWriter
是System.IO
命名空间中的类,用于逐行或逐字符地读取和写入文件。
StreamReader
读取文件using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
using (StreamReader reader = new StreamReader(path))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
StreamReader.ReadLine
方法会逐行读取文件内容,适合处理大文件。
StreamWriter
写入文件using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
using (StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine("Hello, World!");
}
}
}
StreamWriter.WriteLine
方法会将字符串写入文件,并在末尾添加换行符。
FileStream
FileStream
类提供了对文件的低级访问,允许以字节为单位进行读取和写入操作。
FileStream
读取文件using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, bytesRead));
}
}
}
}
FileStream.Read
方法会从文件中读取指定数量的字节到缓冲区中。
FileStream
写入文件using System;
using System.IO;
using System.Text;
class Program
{
static void Main()
{
string path = "example.txt";
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{
string content = "Hello, World!";
byte[] buffer = Encoding.UTF8.GetBytes(content);
fs.Write(buffer, 0, buffer.Length);
}
}
}
FileStream.Write
方法会将字节数组写入文件。
BinaryReader
和BinaryWriter
BinaryReader
和BinaryWriter
类用于以二进制格式读取和写入文件。
BinaryReader
读取文件using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.bin";
using (BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open)))
{
int intValue = reader.ReadInt32();
double doubleValue = reader.ReadDouble();
Console.WriteLine($"Int: {intValue}, Double: {doubleValue}");
}
}
}
BinaryReader
提供了多种方法来读取不同类型的数据,如ReadInt32
、ReadDouble
等。
BinaryWriter
写入文件using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.bin";
using (BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Create)))
{
writer.Write(42);
writer.Write(3.14);
}
}
}
BinaryWriter
提供了多种方法来写入不同类型的数据,如Write(int)
、Write(double)
等。
MemoryMappedFile
MemoryMappedFile
类允许将文件映射到内存中,从而可以像访问内存一样访问文件内容。
MemoryMappedFile
读取文件using System;
using System.IO.MemoryMappedFiles;
class Program
{
static void Main()
{
string path = "example.txt";
using (var mmf = MemoryMappedFile.CreateFromFile(path, FileMode.Open, "mapName"))
{
using (var accessor = mmf.CreateViewAccessor())
{
byte[] buffer = new byte[1024];
accessor.ReadArray(0, buffer, 0, buffer.Length);
Console.WriteLine(Encoding.UTF8.GetString(buffer));
}
}
}
}
MemoryMappedFile.CreateFromFile
方法会将文件映射到内存中,CreateViewAccessor
方法则提供了对映射内存的访问。
MemoryMappedFile
写入文件using System;
using System.IO.MemoryMappedFiles;
using System.Text;
class Program
{
static void Main()
{
string path = "example.txt";
using (var mmf = MemoryMappedFile.CreateFromFile(path, FileMode.Create, "mapName", 1024))
{
using (var accessor = mmf.CreateViewAccessor())
{
string content = "Hello, World!";
byte[] buffer = Encoding.UTF8.GetBytes(content);
accessor.WriteArray(0, buffer, 0, buffer.Length);
}
}
}
}
MemoryMappedFile.CreateFromFile
方法会创建一个新的内存映射文件,CreateViewAccessor
方法则提供了对映射内存的写入操作。
C#提供了多种方式来读取和写入文件,每种方式都有其适用的场景:
File
类适合简单的文件操作,如一次性读取或写入整个文件。StreamReader
和StreamWriter
适合逐行或逐字符地处理文件。FileStream
适合以字节为单位进行文件操作。BinaryReader
和BinaryWriter
适合处理二进制文件。MemoryMappedFile
适合需要将文件映射到内存中进行高效访问的场景。根据具体的需求选择合适的文件操作方式,可以提高代码的效率和可读性。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。