C# StreamReader 标记问题

发布于 2024-10-20 09:54:40 字数 341 浏览 3 评论 0原文

我想做的是记住我在输入流中的位置,然后再返回那里。 在java中使用mark()和reset()非常简单,但我不知道如何在c#中实现这一点。没有这样的方法。

例如

public int peek() 
{
    try 
    {
        file.x; //in java file.mark(1)
        int tmp = file.read();
        file.+ //in java file.reset();
        return tmp;
    } 
    catch (IOException ex) {} 
    return 0;
}

What I am trying to do is remember where I am in an input stream and later go back there.
It is very simple in java using mark() and reset(), but I do not know how to make possible this in c#. There is no such method.

for example

public int peek() 
{
    try 
    {
        file.x; //in java file.mark(1)
        int tmp = file.read();
        file.+ //in java file.reset();
        return tmp;
    } 
    catch (IOException ex) {} 
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-10-27 09:54:40

据我所知确实没有。但是,您可以使用类似 Stack 的东西,然后简单地使用 Push() 和 Pop() 来按顺序上下移动标记:

FileStream file = new FileStream(...);

try {
  Stack<long> markers = new Stack<long>();

  markers.Push(file.Position);

  file.Read(....);

  file.Seek(markers.Pop(),SeekOrigin.Begin);
} finally {
  file.Close();
}

基于字典的另一个想法:

FileStream file = new FileStream(...);

try {
  Dictionary<string,long> markers = new Dictionary<string,long>();

  markers.Add("thebeginning",file.Position);

  file.Read(....);

  file.Seek(markers["thebeginning"],SeekOrigin.Begin);
} finally {
  file.Close();
}

Indeed there isn't that I know of. However you could use something like a Stack and simply Push() and Pop() off this to go up and down your markers in order:

FileStream file = new FileStream(...);

try {
  Stack<long> markers = new Stack<long>();

  markers.Push(file.Position);

  file.Read(....);

  file.Seek(markers.Pop(),SeekOrigin.Begin);
} finally {
  file.Close();
}

Another idea based on a Dictionary:

FileStream file = new FileStream(...);

try {
  Dictionary<string,long> markers = new Dictionary<string,long>();

  markers.Add("thebeginning",file.Position);

  file.Read(....);

  file.Seek(markers["thebeginning"],SeekOrigin.Begin);
} finally {
  file.Close();
}
爱她像谁 2024-10-27 09:54:40

如果您使用的是 StreamReader,则必须记住,它本身并不完全是 Stream,但您可以访问其 BaseStream > 属性:

StreamReader reader = new StreamReader("test.txt");
Stream stream = reader.BaseStream;

它将为您提供您在流中的当前位置:

long pos = stream.Position;

并且允许您移回到那里:

stream.Seek(pos, SeekOrigin.Begin);

If it is a StreamReader you are using, you have to keep in mind that it is not exactly a Stream itself, but you can access its BaseStream property:

StreamReader reader = new StreamReader("test.txt");
Stream stream = reader.BaseStream;

It will give you your current position in the stream:

long pos = stream.Position;

And it will allow you to move back there:

stream.Seek(pos, SeekOrigin.Begin);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文