在某个特定位置写入文件 [英] Writing in a File at Some Specific Position
本文介绍了在某个特定位置写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我们逐行阅读文件,请使用Stream Reader。
现在如果我想在特定位置使用StreamWriter写文件那么如何当我出现在那个特定位置时,我会知道这一点。
任何人都可以通过示例/代码片段分享说明吗??
When by Using the Stream Reader if we read the File Line By Line.
Now If I wants to Write The File using StreamWriter At Some Specific Position Then How I will Come to Know about That When I was Present at that Specific Position.
Can Anyone Please Share The Explanation with an Example/Code Snippet ..?
推荐答案
你可以使用StreamWriter类的BaseStream属性的Seek方法跳转到该位置。
示例代码
You can use the Seek method of the BaseStream property of StreamWriter class to jump to the location.
sample code
FileStream stream = new FileStream(@"filename.txt",FileMode.Open,FileAccess.ReadWrite);
StreamWriter writer = new StreamWriter(stream);
writer.BaseStream.Seek(10000, SeekOrigin.Begin); // where 10000 is the offset to jump
writer.WriteLine("new Test starts here");
检查 MSDN-Seek [ ^ ]和 BaseStream [ ^ ]链接了解更多详情
Check MSDN-Seek[^] and BaseStream[^] links for more details
这篇关于在某个特定位置写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文