C# 将文本文件拆分为较小的文件
我有一个10万条记录的文本文件,所以我想把这个文件分成多个文件,每个文件有100条记录。这是我使用listbox1控制文件的代码。代码正在运行,但缺少的记录较少C# 将文本文件拆分为较小的文件,c#,C#,我有一个10万条记录的文本文件,所以我想把这个文件分成多个文件,每个文件有100条记录。这是我使用listbox1控制文件的代码。代码正在运行,但缺少的记录较少 private void WriteToFile() { int RowCount = listBox1.Items.Count; string FileName = "C:\\Users\\bbdnet0986\\Documents\\MyExpotedQADATA"; Stre
private void WriteToFile()
{
int RowCount = listBox1.Items.Count;
string FileName = "C:\\Users\\bbdnet0986\\Documents\\MyExpotedQADATA";
StreamWriter sw = new StreamWriter(FileName + ".txt");
int inc = 0;
int counter = 0;
//StreamWriter sw = new StreamWriter(FileName+inc + ".txt");
for (int i = 0; i < listBox1.Items.Count; i++)
{
sw.WriteLine(listBox1.Items[i].ToString());
string me = listBox1.Items[i].ToString();
if (RowCount > 100)
{
listBox2.Items.Add(listBox1.Items[counter].ToString());
counter++;
if (counter == 100)
{
inc++;
sw = new StreamWriter(FileName + inc + ".txt");
RowCount = RowCount - counter;
counter = 0;
}
}
else
{
sw.WriteLine(listBox1.Items[i].ToString());
}
}
sw.Close();
}
private void WriteToFile()
{
int RowCount=listBox1.Items.Count;
string FileName=“C:\\Users\\bbdnet0986\\Documents\\MyExpotedQADATA”;
StreamWriter sw=新的StreamWriter(文件名+“.txt”);
int inc=0;
int计数器=0;
//StreamWriter sw=新的StreamWriter(文件名+inc+“.txt”);
对于(int i=0;i100)
{
listBox2.Items.Add(listBox1.Items[counter].ToString());
计数器++;
如果(计数器==100)
{
inc++;
sw=新StreamWriter(文件名+inc+“.txt”);
行计数=行计数-计数器;
计数器=0;
}
}
其他的
{
sw.WriteLine(listBox1.Items[i].ToString());
}
}
sw.Close();
}
在这行中:
sw = new StreamWriter(FileName + inc + ".txt");
您需要对上一个软件执行.Flush()。写入程序被缓冲,这就是某些记录丢失的原因。我不确定您的问题与您的
列表框有何关联,因此我将向您展示一种解决方案,该解决方案每100行从一个大文件创建一个文件
使用Linq
和:
请注意,File.ReadLines
将行流化,而不是全部加载到内存中。这里有一个更简单的方法:
private void WriteToFile()
{
// get an array of strings - you'll find out way :)
string[] items = listBox1.Items.Cast<string>().ToArray();
// this would also work with ReadAllLines()
string[] items = File.ReadAllLines("Your original file");
// path + filename prefix
string fileNamePattern = "C:\\Users\\bbdnet0986\\Documents\\MyExpotedQADATA{0}.txt";
// blocks of 100
string[] buffer;
for(int i = 0; i < items.Length; i += 100)
{
// slice the string array into 100 string blocks
buffer = items.Slice(i, 100);
// output the block of strings
File.WriteAllLines(string.Format(fileNamePattern, i), buffer);
}
}
private void WriteToFile()
{
//获取字符串数组-您将找到方法:)
string[]items=listBox1.items.Cast().ToArray();
//这也适用于ReadAllLines()
string[]items=File.ReadAllLines(“您的原始文件”);
//路径+文件名前缀
string fileNamePattern=“C:\\Users\\bbdnet0986\\Documents\\MyExpotedQADATA{0}.txt”;
//100个街区
字符串[]缓冲区;
对于(int i=0;i
切片扩展:
public static T[] Slice<T>(this T[] source, int index, int length)
{
T[] slice = new T[length];
Array.Copy(source, index, slice, 0, length);
return slice;
}
公共静态T[]片(此T[]源,int索引,int长度)
{
T[]切片=新的T[长度];
复制(源、索引、切片、0、长度);
返回片;
}
列表框不适合或不打算容纳100万个项目。这会不必要地一次将100万行加载到内存中。@TimSchmelter-另一种方法,因为您已经选择了最佳答案。还要注意的是,我的解决方案比LINQ和.NET2.0-3.5(VS2008)友好型更快:)但是,我想我可以使用文件流来代替。
public static T[] Slice<T>(this T[] source, int index, int length)
{
T[] slice = new T[length];
Array.Copy(source, index, slice, 0, length);
return slice;
}