C# 使用程序针对文本文件单行连续多字符内容根据分隔符号转多行方法

1、使用ASP网页,输出结果直接复制粘贴到新文本文件里

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //string str = "aaa,bbb,ccc,ddd";
        // 读取文件的源路径及其读取流
        string strReadFilePath = @"G:\数据001.txt";
        StreamReader srReadFile = new StreamReader(strReadFilePath);
        // 写入文件的源路径及其写入流
        string strWriteFilePath = @"G:\数据002.txt";
        StreamWriter swWriteFile = File.CreateText(strWriteFilePath);

        // 读取流直至文件末尾结束
        while (!srReadFile.EndOfStream)
        {
            string str = srReadFile.ReadLine(); //读取每行数据
            //Console.WriteLine(strReadLine); //屏幕打印每行数据
            string[] sArray = str.Split(',');
            foreach (string i in sArray) //Response.Write(i.ToString() + "<br>");
                swWriteFile.WriteLine(i.ToString()); //写入读取的每行数据
        }

        // 关闭读取流文件
        srReadFile.Close();
        swWriteFile.Close();
    }
}

2、使用WinForm窗体控制动态参数,直接输出结果到新文本文件

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 统计写入(读取的行数)
            int WriteRows = 0;
            //string str = "aaa,bbb,ccc,ddd";
            // 读取文件的源路径及其读取流
            string strReadFilePath = textBox1.Text.ToString();
            StreamReader srReadFile = new StreamReader(strReadFilePath);
            // 写入文件的源路径及其写入流
            string strWriteFilePath = textBox2.Text.ToString();
            StreamWriter swWriteFile = File.CreateText(strWriteFilePath);

            // 读取流直至文件末尾结束
            while (!srReadFile.EndOfStream)
            {
                string str = srReadFile.ReadLine(); //读取每行数据
                //Console.WriteLine(strReadLine); //屏幕打印每行数据
                string[] sArray = str.Split(textBox3.Text.ToCharArray());
                //foreach (string i in sArray) //Response.Write(i.ToString() + "<br>");
                //swWriteFile.WriteLine(i.ToString()); //写入读取的每行数据
                //++WriteRows; //统计写入(读取)的数据行数
                for (int i = 0; i < sArray.Length; i++)
                {
                    swWriteFile.WriteLine(sArray[i].ToString()); //写入读取的每行数据
                    WriteRows=i+1; //统计写入(读取)的数据行数
                }

            }

            label4.Visible = true;
            label4.Text = WriteRows.ToString();
            // 关闭读取流文件
            srReadFile.Close();
            swWriteFile.Close();
        }

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值