题
目前我正在试图找出如何保存的内容一是成流(目前使用的文件流)并且这样做与一些其他的数据。然后我当然希望能够从这种文件。我目前正试图使用一些东西沿着以下行。
FileStream stream = new FileStream(); //this is actually correctly defined.
ASCIIEncoding encoding = new ASCIIEncoding();
//write Title
byte[] array = encoding.GetBytes(Title);
stream.WriteByte(Convert.ToByte(array.Length));
stream.Write(array, 0, array.Length);
//save textRange
textRange.Save(stream, System.Windows.DataFormats.Rtf);
//write Subtitle
byte[] array = encoding.GetBytes(Subtitle);
stream.WriteByte(Convert.ToByte(array.Length));
stream.Write(array, 0, array.Length);
//ect...and something very similar for Loading a file.
这基本上是我想要做的。我实际上节省了2TextRanges和一堆更多的属性。所以我的问题是,TextRange.Load()读取文件的结尾...使得不可能让我的使用,考虑到我有2TextRanges我需要保存/负荷。所以我在这里试图找出另一种方法能够节省/载的内容一是与其他数据。我不需要使用流。我是很开放的任何可行的解决方案。在此先感谢!
~Jasson
解决方案
你可以载/保存到一个流来解决您的问题的阅读来结束的文件。它可能看起来是这样的:
- 载你的文件进入存储器
- 负载部分的文件,该文件的内容是进一流
- 负载的内容是从这一流
或者是你想知道你如何创造和分析文件,以包含不同的部分的标题和内容,以及任何其他领域?
其他提示
我想我应该后我目前的解决方案。这似乎是工作,非常好。谢谢你克里斯和蚂蚁的提示如何去这样做。
/// <summary>
/// Reads a TextRange (DataFormats.Rtf) from the stream.
/// </summary>
/// <param name="stream">The stream to be read from.</param>
/// <returns>The TextRange (DataFormats.Rtf) that was read from the stream.</returns>
public static TextRange ReadTextRange(FileStream stream)
{
long startPos = stream.Position;
int length = -1;
int count = 0;
int previousByte = 0;
int currentByte = 0;
//set previousByte to give the current one something to compare to
previousByte = stream.ReadByte();
//parse the file counting the { and } to find the end of the rtf portion of the file.
while (count > 0 || length < 1)
{
length++;
stream.Position = startPos + length;
currentByte = stream.ReadByte();
if (previousByte != 92) // not '\' so check to see if '{' or '}' is currentByte
{
if (currentByte == 123) // '{' increase count
count++;
else if (currentByte == 125) // '}' decrease count
count--;
}
previousByte = currentByte;
}
//save finish position to move to later
long finishPos = stream.Position;
//reset stream position to start at beginning of rtf
stream.Position = startPos;
//read the rtf portion of the file into a byte[]
byte[] content = new byte[length];
stream.Read(content, 0, length);
//put the byte[] into a memory stream
MemoryStream memStream = new MemoryStream(content);
FlowDocument doc = new FlowDocument();
TextRange range = new TextRange(doc.ContentStart, doc.ContentEnd);
//have the TextRange read from the memorystream
range.Load(memStream, System.Windows.DataFormats.Rtf);
memStream.Close();
//set the position to after the rtf portion of the file
stream.Position = finishPos;
return range;
}
这ReadTextRange方法是在一个StreamHelper I类定义为帮助读一个文件流。所以,所有这一切都是为载入TextRange保存的文件流像这样...
//save query (TextRange)
Query.Save(stream, System.Windows.DataFormats.Rtf);
我希望有人认为这是有用的,如果/当他们来到一个类似的问题!D
编辑:
我用一个探查和发现这种代码不是非常高效的,所以我改变了这个代码的更有效的在几个方面。
而不是使用TextRange和使用一个字节[]保持的内容的流memStream.这削减了的范围。载荷,它消耗了大量的CPU。
我拿出行流。位置=startPos+长度,因为我意识到这是无用之后的第一次运行,还采取了大量的CPU。我放置流。位置--;后行previousByte=流。ReadByte();
此外,我意识到我是一个糟糕的编码器和不以下视通过具有TextRange,UI元,在我的数据类。现在它有一个字节[],其中要好得多。
编辑再次:
几分钟后具有字节[]而不是TextRange我意识到我的尺寸字节[]所以我不需要分析它。因此,不是我写的字节[]大小和随后的byte[]。这使得它非常的快速和可以读一个很大的文件几乎瞬间。