C#中如何进行字符串IO操作

c#
967
2024/4/29 19:01:42
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C#中进行字符串IO操作,可以使用以下代码示例:

  1. 写入字符串到文件:
string text = "Hello, World!";
string path = "test.txt";

// 写入文件
File.WriteAllText(path, text);
  1. 从文件中读取字符串:
string path = "test.txt";

// 读取文件
string text = File.ReadAllText(path);
Console.WriteLine(text);
  1. 使用StreamWriter写入字符串到文件:
string text = "Hello, World!";
string path = "test.txt";

// 使用StreamWriter写入文件
using (StreamWriter writer = new StreamWriter(path))
{
    writer.Write(text);
}
  1. 使用StreamReader从文件中读取字符串:
string path = "test.txt";

// 使用StreamReader读取文件
using (StreamReader reader = new StreamReader(path))
{
    string text = reader.ReadToEnd();
    Console.WriteLine(text);
}

这些示例演示了如何在C#中进行字符串的IO操作,包括写入和读取文件中的字符串。您可以根据实际需求选择适合的方法来操作字符串数据。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: c#四舍五入的方法是什么