0

当我使用 StringReader.ReadLine 从字符串数组中读取文本时,它会完美读取它,但它也会清除我的数组,

using (StringReader reader = new StringReader(filesGroupList[x]))
       {
          while ((filesGroupList[x] = reader.ReadLine()) != null)
             {
                ...
             }
        }

现在,filesGroupList 是空的。所以如果我想再次从这个字符串中读取数据,它会给我空引用异常,所以我唯一的方法是在使用 ReadLine 之前创建这个数组的副本,但是有机会避免它吗?所以当 StringReaded 读完该行时,我的行仍然留在数组中。

4

3 回答 3

1

鉴于您编写的代码,这是我看到的一个示例:

//going to set filesGroupList[x] to a string and then see what happens.
filesGroupList[x] = "First line of string.\nSecond line of string.\n";
//now we go into the using portion.
using (StringReader reader = new StringReader(filesGroupList[x]))
{
  //reader has access to the whole string.
  while ((filesGroupList[x] = reader.ReadLine()) != null)
  {
    //first time through, filesGroupList[x] is set to "First line of string."
    //second time through, filesGroupLIst[x] is set to "Second line of string."
    Console.WriteLine(filesGroupList[x]);
  }
  //third time through, ReadLine() returns null.
  //filesGroupList[x] is set to null.
  //Code breaks out of while loop.
  Console.WriteLine(filesGroupList[x]); //outputs an empty line.
}
//outside of using, filesGroupList[x] still null.
Console.WriteLine(filesGroupList[x]); //also outputs an empty line.

现在,鉴于我建议使用 line 的其他答案,我们将保持所有内容相同,除了 line 部分。

//going to set filesGroupList[x] to a string and then see what happens.
filesGroupList[x] = "First line of string.\nSecond line of string.\n";
using (StringReader reader = new StringReader(filesGroupList[x]))
{
  //reader has access to the whole string.
  string line;
  while ((line = reader.ReadLine()) != null)
  {
    //first time through, line is set to "First line of string."
    //second time through, line is set to "Second line of string."
    Console.WriteLine(line);
  }
  //third time through, ReadLine() returns null.
  //line is set to null.
  //filesGroupList[x] is still set to "First line of string.\nSecond line of string.\n"
  Console.WriteLine(line); //outputs an empty line.
  Console.Write(filesGroupList[x]); //outputs whole string (on 2 lines).
}
Console.WriteLine(line); //won't compile. line doesn't exist.
Console.Write(filesGroupList[x]); //outputs whole string (on 2 lines).

因此,我认为您不想读取filesGroupList[x]然后将其存储在filesGroupList[x]. 如果字符串 infilesGroupList[x]没有行尾字符,则只需将该字符串放回其中(然后null在下一次while循环中放入)。如果 in 中的字符串filesGroupList[x]确实有行尾字符,那么每次通过while循环时,您都会将部分字符串放回filesGroupList[x],我认为这不是您的意图。

于 2013-03-09T19:43:13.870 回答
0

编辑

实际上,在 using 语句之后,字符串为空,这是预期的,因为:

StringReader.Dispose:释放 TextReader 对象使用的所有资源。(继承自 TextReader。)

MSDN 链接在这里

问题是你为什么要这样做,你想做的似乎不合理。没有更多的上下文信息。


您的代码运行完美,或者您无法解释自己,或者您的问题完全不相关。

我自己试过:

static void Main(string[] args)
{
    string[] filesGroupList = new string[1];

    int x = 0;

    filesGroupList[x] = "String is here!";

    using (StringReader reader = new StringReader(filesGroupList[x]))
    {
        while ((filesGroupList[x] = reader.ReadLine()) != null)
        {
            Console.WriteLine("the string is here, I just checked");
            Console.WriteLine(filesGroupList[x]);
        }
    }
    Console.ReadLine();
}

和输出:

字符串在这里,我刚刚检查过

字符串在这里!

于 2013-03-08T16:52:19.097 回答
-1

我认为你的错误就在这里:

while ((filesGroupList[x] = reader.ReadLine()) != null)

您正在更改filesGroupList[x]每次阅读和最后一次阅读的值,将其设置为null.

如果相反,你做了这样的事情:

string line;
while ((line = reader.ReadLine()) != null)
...

然后一旦你在使用之外,你会发现filesGroupList[x]没有变化。

于 2013-03-08T18:06:54.867 回答