StreamReader.ReadToEnd获取的字符串如何反转成文件
时间: 2024-02-01 20:14:48 浏览: 75
要将字符串反转成文件,可以使用以下步骤:
1. 创建一个新的文件并打开它以进行写入(可以使用FileStream类)。
2. 将字符串转换为字节数组(可以使用Encoding类)。
3. 反转字节数组。
4. 将反转后的字节数组写入文件中。
以下是一个示例代码:
```csharp
using System;
using System.IO;
using System.Text;
class Program
{
static void Main(string[] args)
{
string filePath = "example.txt";
string content = "";
using (StreamReader reader = new StreamReader(filePath))
{
content = reader.ReadToEnd();
}
byte[] bytes = Encoding.UTF8.GetBytes(content);
Array.Reverse(bytes);
using (FileStream stream = new FileStream("reversed.txt", FileMode.Create, FileAccess.Write))
{
stream.Write(bytes, 0, bytes.Length);
}
}
}
```
在上面的示例中,我们首先使用StreamReader.ReadToEnd方法读取文件的内容,并将其存储在一个字符串变量中。然后,我们将该字符串转换为字节数组,并使用Array.Reverse方法反转该数组。最后,我们创建一个新的文件并使用FileStream.Write方法将反转后的字节数组写入该文件中。
阅读全文
相关推荐

















