【问题标题】:How to read a file starting at a specific cursor point in C#?如何读取从 C# 中特定光标点开始的文件?
【发布时间】:2010-12-08 20:49:08
【问题描述】:

我想读取一个文件,但不是从文件的开头,而是在文件的特定位置。例如,我想在文件开头的 977 个字符之后读取一个文件,然后一次读取接下来的 200 个字符。谢谢。

【问题讨论】:

    标签: c# file file-io writing


    【解决方案1】:

    如果你想以文本形式读取文件,跳过字符(不是字节):

    using (var textReader = System.IO.File.OpenText(path))
    {
        // read and disregard the first 977 chars
        var buffer = new char[977];
        textReader.Read(buffer, 0, buffer.Length);
    
        // read 200 chars
        buffer = new char[200];
        textReader.Read(buffer, 0, buffer.Length);
    }
    

    如果您只想跳过一定数量的字节(不是字符):

    using (var fileStream = System.IO.File.OpenRead(path))
    {
        // seek to starting point
        fileStream.Seek(977, SeekOrigin.Begin);
    
        // read 200 bytes
        var buffer = new byte[200];
        fileStream.Read(buffer, 0, buffer.Length);
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 Linq 并将 char 数组转换为字符串。

      添加这些命名空间:

      using System.Linq;
      using System.IO;
      

      然后您可以使用它从您的文本文件中获取一个字符数组起始索引 a 和 b 个字符

      char[] c = File.ReadAllText(FilePath).ToCharArray().Skip(a).Take(b).ToArray();
      

      然后你可以有一个字符串,包括 c 的连续字符:

      string r = new string(c);
      

      例如,我在一个文件中有这个文本:

      你好吗?

      我使用这个代码:

      char[] c = File.ReadAllText(FilePath).ToCharArray().Skip(6).Take(3).ToArray();               
      string r = new string(c);
      MessageBox.Show(r);
      

      它显示:如何

      方式二

      非常简单: 使用子串方法

      string s = File.ReadAllText(FilePath);
      string r = s.Substring(6,3);
      MessageBox.Show(r);
      

      祝你好运;

      【讨论】:

        【解决方案3】:
        using (var fileStream = System.IO.File.OpenRead(path))
        {
            // seek to starting point
            fileStream.Position = 977;
        
            // read
        }
        

        【讨论】:

          【解决方案4】:

          如果你想从文件中读取特定的数据类型,System.IO.BinaryReader 是最好的选择。 如果您不确定文件编码使用

                  using (var binaryreader = new BinaryReader(File.OpenRead(path)))
                  {
                      // seek to starting point
                      binaryreader.ReadChars(977);
                      // read
                      char[] data = binaryreader.ReadChars(200);
                      //do what you want with data
                  }
          

          如果你知道源文件中的字符大小为 1 或 2 字节,则使用

                  using (var binaryreader = new BinaryReader(File.OpenRead(path)))
                  {
                      // seek to starting point
                      binaryreader.BaseStream.Position = 977 * X;//x is 1 or 2 base on character size in sourcefile
                      // read
                      char[] data = binaryreader.ReadChars(200);
                      //do what you want with data
                  }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-04-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-09-13
            • 2019-01-14
            相关资源
            最近更新 更多