C# saves the single and double lines in the text text separately

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


text segmentation

1. Set the name of the text file to 0

insert image description here

code show as below:

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);
        }
    }
}

2. Text export

insert image description here

Guess you like

Origin blog.csdn.net/m0_56265052/article/details/132509476