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.文本导出

相关推荐
mudtools9 小时前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的19 小时前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清218821 小时前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理1 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi1 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1231 天前
matlab画图工具
开发语言·matlab
dustcell.1 天前
haproxy七层代理
java·开发语言·前端
norlan_jame1 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone1 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054961 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django