如何实现倒退读写
例如将file1文件中的ABCD写到file2中变成DCBA?(不要用数组和Seek()方法)尽量多说些!
程序代码:using System;
using System.Collections.Generic;
using System.Text;
using
namespace ConsoleApplication1
{
public class FileInvertedSequence
{
private FileStream stringFile;
private string fileName;
private string ReadStringInFile()
{
StreamReader sr = new StreamReader(stringFile);
string allString = sr.ReadToEnd();
sr.Close();
stringFile.Close();
return allString;
}
//重新排序
public string InvertedSequenceString()
{
string orderString = ReadStringInFile();
char[] chars = orderString.ToCharArray();
StringBuilder sb = new StringBuilder();
for (int i = chars.Length - 1; i >= 0; i--)
{
sb.Append(chars[i].ToString());
}
return sb.ToString();
}
public FileInvertedSequence(string FilePath)
{
this.fileName = FilePath;
this.stringFile = new FileStream(this.fileName, FileMode.Open);
}
}
class Program
{
static void Main(string[] args)
{
FileInvertedSequence test = new FileInvertedSequence("test.txt");
Console.Write(test.InvertedSequenceString());
Console.ReadKey();
}
}
}
先将文件中的字符串都读出来,然后根据ToCharArray()方法,逆序输出。