C# 实现将一个文本文档按行数分成多个文档
2014-01-12 10:12
330 查看
比如一个文本文档里有7行数据,将它每两行或者每三行等等分成多个新的文档
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace ConsoleApplication1
{
public class Program
{
//变量 TotalCountInEveryFile 表示每个新建文档里的行数
private const int TotalCountInEveryFile = 3;
static void Main(string[] args)
{
ArrayList gotStrings = GetStreamMethod("d:\\Hello.txt");
Console.WriteLine(string.Join("\n", gotStrings.ToArray()));
if (gotStrings != null)
{
int fileCount = 1;
for (int i = 0; i < gotStrings.Count; i++)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}\r\n", gotStrings[i]);
if (i + 1 < gotStrings.Count)
{
i++;
}
else
{
WriteStreamMethod(string.Format("d:\\Document{0}.txt", fileCount), sb.ToString());
return;
}
while (i % TotalCountInEveryFile != 0)
{
sb.AppendFormat("{0}\r\n", gotStrings[i]);
if (i != gotStrings.Count - 1)
{
i++;
}
else
{
WriteStreamMethod(string.Format("d:\\Document{0}.txt", fileCount), sb.ToString());
return;
}
}
Console.WriteLine(sb.ToString());
WriteStreamMethod(string.Format("d:\\Document{0}.txt", fileCount), sb.ToString());
if (i % TotalCountInEveryFile == 0)
{
i--;
}
fileCount++;
}
}
}
// 读
private static ArrayList GetStreamMethod(string path)
{
ArrayList list = new ArrayList();
StreamReader sr = new StreamReader(path);
String line;
while ((line = sr.ReadLine()) != null)
{
list.Add(line.ToString());
}
return list;
}
//写
private static void WriteStreamMethod(string path, string content)
{
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(content);
sw.Flush();
sw.Close();
fs.Close();
}
}
}
using System;
using System.Collections;
using System.IO;
using System.Text;
namespace ConsoleApplication1
{
public class Program
{
//变量 TotalCountInEveryFile 表示每个新建文档里的行数
private const int TotalCountInEveryFile = 3;
static void Main(string[] args)
{
ArrayList gotStrings = GetStreamMethod("d:\\Hello.txt");
Console.WriteLine(string.Join("\n", gotStrings.ToArray()));
if (gotStrings != null)
{
int fileCount = 1;
for (int i = 0; i < gotStrings.Count; i++)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}\r\n", gotStrings[i]);
if (i + 1 < gotStrings.Count)
{
i++;
}
else
{
WriteStreamMethod(string.Format("d:\\Document{0}.txt", fileCount), sb.ToString());
return;
}
while (i % TotalCountInEveryFile != 0)
{
sb.AppendFormat("{0}\r\n", gotStrings[i]);
if (i != gotStrings.Count - 1)
{
i++;
}
else
{
WriteStreamMethod(string.Format("d:\\Document{0}.txt", fileCount), sb.ToString());
return;
}
}
Console.WriteLine(sb.ToString());
WriteStreamMethod(string.Format("d:\\Document{0}.txt", fileCount), sb.ToString());
if (i % TotalCountInEveryFile == 0)
{
i--;
}
fileCount++;
}
}
}
// 读
private static ArrayList GetStreamMethod(string path)
{
ArrayList list = new ArrayList();
StreamReader sr = new StreamReader(path);
String line;
while ((line = sr.ReadLine()) != null)
{
list.Add(line.ToString());
}
return list;
}
//写
private static void WriteStreamMethod(string path, string content)
{
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(content);
sw.Flush();
sw.Close();
fs.Close();
}
}
}
相关文章推荐
- c#调用COM组件
- c#中虚函数的相关使用方法
- 浅析C# web访问mysql数据库-整理归纳总结
- C# IP地址与整数之间转换的具体方法
- C#实现的阴历阳历互相转化类实例
- C# 的关键字详细介绍
- c# 获得局域网主机列表实例
- C#中Convert.ToInt32()和int.Parse()的区别介绍
- C#面向对象特征的具体实现及作用详解
- 关于C# Math 处理奇进偶不进的实现代码
- c# 随机函数的使用详解
- c#之圆形无标题栏椭圆窗体的实现详解
- C#页面之间跳转功能的小结
- 浅析C# 基础语法的使用
- C#中将字符串转换为整型的三种解决方法总结
- 深入c# GDI+简单绘图的具体操作步骤(一)
- C#中英文混合字符串截取函数
- C# 面向对象的基本原则
- 使用C#开源文件实时监控工具Tail&TailUI介绍
- C# 正则判断一个数字的格式是否有逗号的代码