2014-08-27 59 views
0

我写了这个代码,但也存在一些问题:写一个字符串数组到一个文件,但它只是打印System.String []

const int maxPeopleInFile = 2; 

    using (StreamReader reader = new StreamReader(@"c:\mytest\SortedTest.txt")) 
    { 
     string[] columnheaders = reader.ReadLine().Split(','); 
     List<string> listKeeper = new List<string>(); 
     int fileNumber = 1; 

     while (reader.Peek() > 0) 
     { 
      string[] currentRowValues = reader.ReadLine().Split(','); 
      string id = currentRowValues[2]; 

      if (listKeeper.Count < maxPeopleInFile || (listKeeper.Count() <= maxPeopleInFile && listKeeper.Contains(id))) 
      { 
       if (!listKeeper.Contains(id)) 
       { 
        listKeeper.Add(id); 
       } 

       var writer = File.CreateText("file_" + fileNumber + ".txt"); 
       writer.Write(currentRowValues); 
       writer.Close(); 
      } 
      else // new file 
      { 
       fileNumber++; 
       listKeeper = new List<string>(); 
       var writer = File.CreateText("file_" + fileNumber + ".txt"); 
       writer.Write(currentRowValues); 
      } 
     } 
    } 

问题: 1:生成的文件不具有行我在string[] currentRowValues = reader.ReadLine().Split(','); 读什么正被写入该文件是一条线,它是文本System.String[]

+2

'writer.Write(currentRowValues)'不重复的值;它基本上做'currentRowValues.ToString()',这不是你想要的。但是,我不清楚为什么你会先把这条线分开,如果你只是想再写一遍...... – 2014-08-27 14:53:05

+0

您是否尝试过使用string.Join语句,如'writer.Write(string.Join(“\\ n”,currentRowValues));' – xDaevax 2014-08-27 14:55:18

+0

除了写入数组类型而不是内容的错误之外,我认为您的循环将永远不会工作。 File.CreateText将用相同的名称覆盖以前的文件。您将2人存储在单个文件中的计划无法正常工作。 – Steve 2014-08-27 15:01:08

回答

1

这是因为currentRowValues不会被序列化回逗号分隔的值列表,因为它将ToString作为对象类型名称的返回值写入FileStream.Write

一个可行的方法可能会使用string.Join

writer.Write(string.Join(",", currentRowValues)); 
+0

是否有任何危险!这个角落案件?例如,如果原始文件的某些部分没有任何值,嗯,但我想他们会在数组中,然后当我们使用Join将它们转换回来时,它们会再次变好吗?只是想确保没有棘手的情况发生后 – ConfusedSleepyDeveloper 2014-08-27 15:02:47

+0

@ConfusedSleepyDeveloper没有问题,这将导致一个空字符串数组 – 2014-08-27 15:03:43

+0

@ConfusedSleepyDeveloper检查此链接https://dotnetfiddle.net/sOZHIH。这是一个关于你的问题的实时示范:) – 2014-08-27 15:04:57

2

由于currentRowValues是一个数组,这个调用

writer.Write(currentRowValues); 

相当于

writer.Write(currentRowValues.ToString()); 

产生,你看到的输出(即System.String[]),因为ToString()确实是而不是迭代单个字符串值。

您可以编写整个数组到一个文件中一次性使用File.WriteAllLines,像这样:

File.WriteAllLines("file_" + fileNumber + ".txt", currentRowValues); 

(从评论) [I]想把[确切行,我从原始文件]中的一个新的文件中读取酷似它曾经是原始文件

那么你应该使用string.Join撤消string.Split的影响:

writer.Write(string.Join(",", currentRowValues)); 
+1

我认为'currentRowValues'只是一行的字段,所以'WriteAllLines'没有帮助。我猜OP想用逗号或不同的分隔符再次加入这些字段。 – 2014-08-27 14:56:48

+0

是的,我从原始文件中读取的确切行,要将其放入与原始文件中的文件完全相同的新文件中 – ConfusedSleepyDeveloper 2014-08-27 14:57:42

0

你的问题是在这个片段:

writer.Write(currentRowValues); 

尝试:

writer.Write(currentRowValues[0]); 
or 
    writer.Write(currentRowValues[1]); 

根据您的需要

0

您需要提供一个string价值的Write()方法,不是string[]

我建议如下:

StringBuilder builder = new StringBuilder(); 
foreach (var currentValue in currentRowValues) 
    builder.Append(currentValue); 
writer.Write(builder.ToString()); 

注:不要忘记添加命名空间,以便能够使用StringBuilder。

相关问题