Read file from position

[亡魂溺海] 提交于 2019-12-25 04:18:09

问题


FileStream infile = new FileStream(@"C:\Users\John\Desktop\ProjectNew\nov.txt",     FileMode.Open, FileAccess.Read);
        int position = x.Length;
        infile.Seek(position, SeekOrigin.Begin);

But Seek method returns number. How to read the file 'infile' from position to end in a string?


回答1:


Is this what you're after? Assuming you wanted to start reading from position 100...

       using (FileStream fs = new FileStream(@"file.txt", FileMode.Open, FileAccess.Read))
        {
            fs.Seek(100, SeekOrigin.Begin);

            byte[] b = new byte[fs.Length - 100];
            fs.Read(b, 0, (int)(fs.Length - 100));

            string s = System.Text.Encoding.UTF8.GetString(b);
        }



回答2:


The Seek method is supposed to return a number, the new position in the stream. Now just call whatever Read function you want.




回答3:


Seek() only places the file pointer somewhere else. If you do a read before the seek, it will read from the beginning of the file. If you read after the seek, it will start reading from position.

So to read the file from position to the end, do Seek(), followed by Read() or ReadToEnd().



来源:https://stackoverflow.com/questions/7596302/read-file-from-position

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!