我写了这个代码,但也存在一些问题:写一个字符串数组到一个文件,但它只是打印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[]
'writer.Write(currentRowValues)'不重复的值;它基本上做'currentRowValues.ToString()',这不是你想要的。但是,我不清楚为什么你会先把这条线分开,如果你只是想再写一遍...... – 2014-08-27 14:53:05
您是否尝试过使用string.Join语句,如'writer.Write(string.Join(“\\ n”,currentRowValues));' – xDaevax 2014-08-27 14:55:18
除了写入数组类型而不是内容的错误之外,我认为您的循环将永远不会工作。 File.CreateText将用相同的名称覆盖以前的文件。您将2人存储在单个文件中的计划无法正常工作。 – Steve 2014-08-27 15:01:08