提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 文本的分割
- 1.设定text文件的名称为0
- 2.文本导出
文本的分割
1.设定text文件的名称为0
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp1
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){string inputFile = @" C:\Users\Administrator\Desktop\0.txt";string oddLinesFile = @" C:\Users\Administrator\Desktop\1.txt";string evenLinesFile = @" C:\Users\Administrator\Desktop\2.txt";// 读取输入文件中的所有行string[] lines = File.ReadAllLines(inputFile);// 创建奇数行和偶数行的文件流using (StreamWriter oddWriter = new StreamWriter(oddLinesFile))using (StreamWriter evenWriter = new StreamWriter(evenLinesFile)){// 遍历所有行for (int i = 0; i < lines.Length; i++){// 奇数行写入奇数行文件if (i % 2 == 0){oddWriter.WriteLine(lines[i]);}// 偶数行写入偶数行文件else{evenWriter.WriteLine(lines[i]);}}}Console.WriteLine("奇数行已保存到 " + oddLinesFile);Console.WriteLine("偶数行已保存到 " + evenLinesFile);}}
}