C#将text文本中的单双行分开单独保存

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


文本的分割

1.设定text文件的名称为0

代码如下:

csharp 复制代码
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.文本导出

相关推荐
manyikaimen11 小时前
博派智能-运动控制技术-RTCP-五轴联动
c++·图像处理·qt·算法·计算机视觉·机器人·c#
Ssan PRIN11 小时前
Modbus报文详解
服务器·开发语言·php
枫叶丹411 小时前
【HarmonyOS 6.0】ArkUI Swiper 组件:深入掌握滑动状态变化事件回调
开发语言·华为·harmonyos
小陈工11 小时前
2026年4月4日技术资讯洞察:异步编程范式重塑、架构理性回归与开发者体验革命
开发语言·人工智能·python·机器学习·架构·数据挖掘·回归
CAE虚拟与现实12 小时前
Python在Windows 平台上引入了一种新的安装与运行时管理机制
开发语言·python
Hello eveybody12 小时前
图论简述+图论考试要点(Python)
开发语言·python·图论
Hello eveybody12 小时前
二叉树简述+考试要点(Python)
开发语言·python
第二只羽毛12 小时前
C++高性能内存池6(面试题)
大数据·开发语言·c++
故事和你9112 小时前
洛谷-算法1-1-模拟与高精度4
开发语言·数据结构·c++·算法·蓝桥杯·动态规划
I love studying!!!12 小时前
Python项目:使用API
开发语言·python