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

相关推荐
551只玄猫1 小时前
【数学建模 matlab 实验报告12】聚类分析和判别分析
开发语言·数学建模·matlab·课程设计·聚类·实验报告
小陈工3 小时前
Python Web开发入门(十七):Vue.js与Python后端集成——让前后端真正“握手言和“
开发语言·前端·javascript·数据库·vue.js·人工智能·python
H Journey3 小时前
C++之 CMake、CMakeLists.txt、Makefile
开发语言·c++·makefile·cmake
rockey6276 小时前
AScript如何实现中文脚本引擎
c#·.net·script·eval·expression·function·动态脚本
lly2024067 小时前
C 标准库 - `<stdio.h>`
开发语言
沫璃染墨7 小时前
C++ string 从入门到精通:构造、迭代器、容量接口全解析
c语言·开发语言·c++
jwn9997 小时前
Laravel6.x核心特性全解析
开发语言·php·laravel
迷藏4947 小时前
**发散创新:基于Rust实现的开源合规权限管理框架设计与实践**在现代软件架构中,**权限控制(RBAC)** 已成为保障
java·开发语言·python·rust·开源
功德+n8 小时前
Linux下安装与配置Docker完整详细步骤
linux·运维·服务器·开发语言·docker·centos
明日清晨8 小时前
python扫码登录dy
开发语言·python