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

相关推荐
Felven1 天前
C. Brr Brrr Patapim
c语言·开发语言
路溪非溪1 天前
python标准库使用总结
开发语言·python
SomeOtherTime1 天前
Rust 面向对象介绍(AI回答)
开发语言·python·rust
zh_xuan1 天前
kotlin常见面试题
开发语言·kotlin·面试题
草莓熊Lotso1 天前
【C++ 基于微服务的即时通讯系统】文件存储子服务实战:UUID生成、文件IO封装与全接口单元测试
开发语言·网络·数据库·c++·微服务·架构·单元测试
huaqianzkh1 天前
WinForm C#:循环修改控件UI闪烁卡顿?SuspendLayout / ResumeLayout 与 BeginUpdate 优化
开发语言·ui·c#
沐晓时光2 天前
C语言入门,内存函数知识讲解
c语言·开发语言
David猪大卫2 天前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
用户3721574261352 天前
C# 实现 Excel 单元格保护:锁定公式并允许编辑指定区域
c#·ai编程·编程语言
fengci.2 天前
CTF2MISC中等部分
开发语言·前端·学习