我是c#初学者,向大家请教一下C#分割文件
我的文件比较大,格式是这样的
line 10
1111 2222 3333 3333 3333
2222 3333 3333 3333 3333
.....
line 11
1111 2222 3333 3333 3333
2222 3333 3333 3333 3333
......
...
怎样将文件按线号分割成多个单独的文件
我的文件比较大,格式是这样的
line 10
1111 2222 3333 3333 3333
2222 3333 3333 3333 3333
.....
line 11
1111 2222 3333 3333 3333
2222 3333 3333 3333 3333
......
...
怎样将文件按线号分割成多个单独的文件
解决方案 »
- 在c# 窗体中如何嵌入Excel,并完成相应的修改和保存?
- 关于反射的问题
- JAVASCRIPT 来实现:当我在下拉列表中选择了第几周(如20)后,立刻显示该周的星期一到星期日的日期(如:3月10 到 3月17)?
- DataGridViewButtonColumn对象设置问题
- 水晶报表如何过滤分别汇总(特急!)
- 鼠标控制DataGrid滚动条
- Timer控件失常问题,请帮忙!急!急!急!急!!!!!!!
- tabControl 在xp下标题背景透明 在2003系统下不可以,求解决
- 如何运用office编程进行格式转换
- 关于button之间的相互调用《急,谢谢各位哥哥姐姐》在线等。。。
- c#调用webservice(wsdl)提示无法连接到远程服务器
- 求思路与代码、
如果文件较大,不建议完全读到内存中再分析。如果是顺序分块的话,可以一行一行读using System;
using System.IO;class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
//判读是不是新的块,如果是则保存前面的块。
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}代码主要部分来自msdn:
http://msdn.microsoft.com/zh-cn/library/db5x7c0d(v=vs.80).aspx
如果你的文件太大可能性能方面不太好
StreamReader sr = new StreamReader(Path);
string[] s = new string[]{"\r\n"};
string[] strs = sr.ReadToEnd.Split(s, StringSplitOptions.RemoveEmptyEntries);
sr.Close();
string strPath = string.Empty;foreach(string str in strs)
{
if ( str.Length < 8)
{
string newStr = str.Substring(0, 4);
if (newStr.Equals("Line"))
{
strPath = Directory + "\\" + strs[i] + ".txt"; //这里根据需要改动地址和后缀
using (System.IO.File.Create(strPath)) { }
}
}
else
{
if (!string.IsNullOrEmpty(strPath))
{
StreamWriter sw = new StreamWriter(strPath, false);
sw.WriteLine(str, true); sw.Close();
}
}
}
StreamReader sr = new StreamReader(Path);
string[] s = new string[]{"\r\n"};
string[] strs = sr.ReadToEnd.Split(s, StringSplitOptions.RemoveEmptyEntries);
sr.Close();
string strPath = string.Empty;foreach(string str in strs)
{
if ( str.Length < 8)
{
string newStr = str.Substring(0, 4);
if (newStr.Equals("Line"))
{
strPath = Directory + "\\" + strs[i] + ".txt"; //这里根据需要改动地址和后缀
using (System.IO.File.Create(strPath)) { }
}
}
else
{
if (!string.IsNullOrEmpty(strPath))
{
StreamWriter sw = new StreamWriter(strPath, false);
sw.WriteLine(str, true); sw.Close();
}
}
}
</pre>
string[] s = new string[]{"\r\n"};
string[] strs = sr.ReadToEnd.Split(s, StringSplitOptions.RemoveEmptyEntries);
sr.Close();
string strPath = string.Empty;foreach(string str in strs)
{
if ( str.Length < 8)
{
string newStr = str.Substring(0, 4);
if (newStr.Equals("Line"))
{
strPath = Directory + "\\" + strs[i] + ".txt"; //这里根据需要改动地址和后缀using (System.IO.File.Create(strPath)) { }
}
}
else
{
if (!string.IsNullOrEmpty(strPath))
{
StreamWriter sw = new StreamWriter(strPath, false);
sw.WriteLine(str, true);sw.Close();
}
}
}
还是不要一次性全部读出来的好,
循环读取,每读到"line 数字"另存为一个文件.
我的文件比较大,如果用 sr.ReadToEnd(),会报错,说是内存不足什么的,所以还是感觉循环读取比较好点,我不知道怎么读到“line 数字”的时候另存为一个文件,再次请教
System.IO.StreamReader sr = new System.IO.StreamReader("c:\\1.txt");
string strTemp = sr.ReadLine();
while (!sr.EndOfStream) {
if (strTemp.StartsWith("line")) {
System.IO.StreamWriter sw = new System.IO.StreamWriter("c:\\" + strTemp.Replace(" ", "_") + ".txt", true);
sw.WriteLine(strTemp);
strTemp = sr.ReadLine();
while (!sr.EndOfStream) {
if (strTemp.StartsWith("line")) {
break; // TODO: might not be correct. Was : Exit While
} else {
sw.WriteLine(strTemp);
}
strTemp = sr.ReadLine();
}
sw.Close();
}
}
sr.Close();
MessageBox.Show("ok");