zoukankan      html  css  js  c++  java
  • txt数据处理

    主要有两个功能

    1.删除两个字符串及其中间的数据。

    2.删除特定的字符串。

    如图所示:

    Program.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WinFormsApp1
    {
        static class Program
        {
            /// <summary>
            ///  The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.SetHighDpiMode(HighDpiMode.SystemAware);
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
    View Code

    Form.cs:

    using System;
    using System.Collections;
    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 WinFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            
    
            private void Button1_Click(object sender, EventArgs e)
            {
                this.openFileDialog1.Filter = "txt文件(*.txt)|*.txt";
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string PicFileName = this.openFileDialog1.FileName;
                    this.imgList.Add(PicFileName);
                   //  this.imageList1.Images.Add(Image.FromFile(PicFileName));
                    textBox3.Text = PicFileName;
                }
            }
    
            private void TextBox3_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void Button2_Click(object sender, EventArgs e)
            {
                this.openFileDialog1.Filter = "txt文件(*.txt)|*.txt";
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string PicFileName = this.openFileDialog1.FileName;
                    this.imgList.Add(PicFileName);
                    //  this.imageList1.Images.Add(Image.FromFile(PicFileName));
                    textBox4.Text = PicFileName; //另存为的路径
                }
            }
    
            //  public IOException e;
            public String str3;
            private string Linesplit(string str1,string str2) {
    
                
                int int1, int2;
                
                int1 = str1.IndexOf(str2);
                int2 = str2.Length;
                if (int2 <= 0 || int1 < 0 ){
                    return str1;
                }
                try
                {
                    str1 = str1.Remove(int1, int2);
                }
                catch (Exception e)
                {
                    throw e;
                }
                //int1 = str1.IndexOf(str2);
                while (str1.IndexOf(str2) > 0)
                {
                    str1 = Linesplit(str1,str2);
                    
                }
                return str1;
            }
            private void Button3_Click(object sender, EventArgs e)
            {
                //IOException ea;
                    try{
                    //读取文件
                        StreamReader fsreader = new StreamReader(textBox3.Text,Encoding.Default);
                        String line;
                    //检查另存为路径文件是否存在,否则创建该文件
                    if (File.Exists(textBox4.Text)){
                        Console.WriteLine("{0} already exists", textBox4.Text);
                    } else{ 
                        
                    }
                    //FileStream fs = new FileStream(textBox4.Text, FileMode.Create);
                    StreamWriter fs = File.CreateText(textBox4.Text);
    
                    while ((line = fsreader.ReadLine()) != null){
                        // Console.WriteLine(line.ToString());
                        //当从原始文档读取到值的时候
                        int int1,int2;
                        int1 = line.IndexOf(textBox1.Text);
                        int2 = -1;
                        if (textBox2.Text != "")
                        {
                            int2 = line.LastIndexOf(textBox2.Text);
                        }
                        
                        if ((int2 >= int1) && int1 > 0)
                        {
                            int2 = int2 - int1 + 1;
                            line = line.Remove(int1,int2);
                            //获得字节数组
                           // byte[] data = System.Text.Encoding.Default.GetBytes(line);
                            //开始写入
                            
                        }
                        line = Linesplit(line, textBox5.Text);
                        line = Linesplit(line, textBox6.Text);
                        line = Linesplit(line, textBox7.Text);
                        line = Linesplit(line, textBox8.Text);
                        line = Linesplit(line, textBox9.Text);
                        line = Linesplit(line, textBox10.Text);
                        //line.Replace(textBox5,"");
                        fs.WriteLine(line);
                    }
                    //清空缓冲区、关闭流
                    fs.Flush();
                    fs.Close();
                    MessageBox.Show("已完成!");
                }
                
                    catch (IOException ea)
                    {
                        Console.WriteLine(ea.ToString());
                    }
                
    
            }
    
        }
    }
    View Code

     Form1.Designer.cs:

    namespace WinFormsApp1
    {
        partial class Form1
        {
            /// <summary>
            ///  Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            ///  Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            ///  Required method for Designer support - do not modify
            ///  the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.label1 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.label3 = new System.Windows.Forms.Label();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.textBox2 = new System.Windows.Forms.TextBox();
                this.textBox3 = new System.Windows.Forms.TextBox();
                this.button1 = new System.Windows.Forms.Button();
                this.label4 = new System.Windows.Forms.Label();
                this.textBox4 = new System.Windows.Forms.TextBox();
                this.label5 = new System.Windows.Forms.Label();
                this.button2 = new System.Windows.Forms.Button();
                this.button3 = new System.Windows.Forms.Button();
                this.label6 = new System.Windows.Forms.Label();
                this.textBox5 = new System.Windows.Forms.TextBox();
                this.textBox6 = new System.Windows.Forms.TextBox();
                this.textBox7 = new System.Windows.Forms.TextBox();
                this.textBox8 = new System.Windows.Forms.TextBox();
                this.textBox9 = new System.Windows.Forms.TextBox();
                this.textBox10 = new System.Windows.Forms.TextBox();
                this.label7 = new System.Windows.Forms.Label();
                this.label8 = new System.Windows.Forms.Label();
                this.label9 = new System.Windows.Forms.Label();
                this.label10 = new System.Windows.Forms.Label();
                this.label11 = new System.Windows.Forms.Label();
                this.label12 = new System.Windows.Forms.Label();
                this.label13 = new System.Windows.Forms.Label();
                this.SuspendLayout();
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(62, 37);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(226, 17);
                this.label1.TabIndex = 0;
                this.label1.Text = "删除原始文档中从‘文本1’到‘文本2’的内容";
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(62, 54);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(51, 17);
                this.label2.TabIndex = 1;
                this.label2.Text = "文本1:";
                // 
                // label3
                // 
                this.label3.AutoSize = true;
                this.label3.Location = new System.Drawing.Point(62, 100);
                this.label3.Name = "label3";
                this.label3.Size = new System.Drawing.Size(51, 17);
                this.label3.TabIndex = 2;
                this.label3.Text = "文本2:";
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(62, 74);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(609, 23);
                this.textBox1.TabIndex = 3;
                // 
                // textBox2
                // 
                this.textBox2.Location = new System.Drawing.Point(62, 120);
                this.textBox2.Name = "textBox2";
                this.textBox2.Size = new System.Drawing.Size(609, 23);
                this.textBox2.TabIndex = 4;
                // 
                // textBox3
                // 
                this.textBox3.Location = new System.Drawing.Point(62, 166);
                this.textBox3.Name = "textBox3";
                this.textBox3.Size = new System.Drawing.Size(559, 23);
                this.textBox3.TabIndex = 5;
                this.textBox3.TextChanged += new System.EventHandler(this.TextBox3_TextChanged);
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(627, 166);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(73, 22);
                this.button1.TabIndex = 6;
                this.button1.Text = "选择文件";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.Button1_Click);
                // 
                // label4
                // 
                this.label4.AutoSize = true;
                this.label4.Location = new System.Drawing.Point(62, 146);
                this.label4.Name = "label4";
                this.label4.Size = new System.Drawing.Size(68, 17);
                this.label4.TabIndex = 7;
                this.label4.Text = "原始文档:";
                // 
                // textBox4
                // 
                this.textBox4.Location = new System.Drawing.Point(62, 212);
                this.textBox4.Name = "textBox4";
                this.textBox4.Size = new System.Drawing.Size(559, 23);
                this.textBox4.TabIndex = 8;
                // 
                // label5
                // 
                this.label5.AutoSize = true;
                this.label5.Location = new System.Drawing.Point(62, 192);
                this.label5.Name = "label5";
                this.label5.Size = new System.Drawing.Size(68, 17);
                this.label5.TabIndex = 9;
                this.label5.Text = "最终文档:";
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(627, 213);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(73, 22);
                this.button2.TabIndex = 10;
                this.button2.Text = "选择路径";
                this.button2.UseVisualStyleBackColor = true;
                this.button2.Click += new System.EventHandler(this.Button2_Click);
                // 
                // button3
                // 
                this.button3.Location = new System.Drawing.Point(62, 441);
                this.button3.Name = "button3";
                this.button3.Size = new System.Drawing.Size(638, 40);
                this.button3.TabIndex = 11;
                this.button3.Text = "开始";
                this.button3.UseVisualStyleBackColor = true;
                this.button3.Click += new System.EventHandler(this.Button3_Click);
                // 
                // label6
                // 
                this.label6.AutoSize = true;
                this.label6.Location = new System.Drawing.Point(62, 247);
                this.label6.Name = "label6";
                this.label6.Size = new System.Drawing.Size(152, 17);
                this.label6.TabIndex = 12;
                this.label6.Text = "删除原始文档下列特定的值";
                // 
                // textBox5
                // 
                this.textBox5.Location = new System.Drawing.Point(109, 267);
                this.textBox5.Name = "textBox5";
                this.textBox5.Size = new System.Drawing.Size(609, 23);
                this.textBox5.TabIndex = 13;
                // 
                // textBox6
                // 
                this.textBox6.Location = new System.Drawing.Point(109, 296);
                this.textBox6.Name = "textBox6";
                this.textBox6.Size = new System.Drawing.Size(609, 23);
                this.textBox6.TabIndex = 14;
                // 
                // textBox7
                // 
                this.textBox7.Location = new System.Drawing.Point(109, 325);
                this.textBox7.Name = "textBox7";
                this.textBox7.Size = new System.Drawing.Size(609, 23);
                this.textBox7.TabIndex = 15;
                // 
                // textBox8
                // 
                this.textBox8.Location = new System.Drawing.Point(109, 354);
                this.textBox8.Name = "textBox8";
                this.textBox8.Size = new System.Drawing.Size(609, 23);
                this.textBox8.TabIndex = 16;
                // 
                // textBox9
                // 
                this.textBox9.Location = new System.Drawing.Point(109, 383);
                this.textBox9.Name = "textBox9";
                this.textBox9.Size = new System.Drawing.Size(609, 23);
                this.textBox9.TabIndex = 17;
                // 
                // textBox10
                // 
                this.textBox10.Location = new System.Drawing.Point(109, 412);
                this.textBox10.Name = "textBox10";
                this.textBox10.Size = new System.Drawing.Size(609, 23);
                this.textBox10.TabIndex = 18;
                // 
                // label7
                // 
                this.label7.AutoSize = true;
                this.label7.Location = new System.Drawing.Point(62, 273);
                this.label7.Name = "label7";
                this.label7.Size = new System.Drawing.Size(39, 17);
                this.label7.TabIndex = 19;
                this.label7.Text = "文本3";
                // 
                // label8
                // 
                this.label8.AutoSize = true;
                this.label8.Location = new System.Drawing.Point(62, 302);
                this.label8.Name = "label8";
                this.label8.Size = new System.Drawing.Size(39, 17);
                this.label8.TabIndex = 20;
                this.label8.Text = "文本4";
                // 
                // label9
                // 
                this.label9.AutoSize = true;
                this.label9.Location = new System.Drawing.Point(62, 418);
                this.label9.Name = "label9";
                this.label9.Size = new System.Drawing.Size(39, 17);
                this.label9.TabIndex = 21;
                this.label9.Text = "文本8";
                // 
                // label10
                // 
                this.label10.AutoSize = true;
                this.label10.Location = new System.Drawing.Point(62, 389);
                this.label10.Name = "label10";
                this.label10.Size = new System.Drawing.Size(39, 17);
                this.label10.TabIndex = 22;
                this.label10.Text = "文本7";
                // 
                // label11
                // 
                this.label11.AutoSize = true;
                this.label11.Location = new System.Drawing.Point(62, 360);
                this.label11.Name = "label11";
                this.label11.Size = new System.Drawing.Size(39, 17);
                this.label11.TabIndex = 23;
                this.label11.Text = "文本6";
                // 
                // label12
                // 
                this.label12.AutoSize = true;
                this.label12.Location = new System.Drawing.Point(62, 331);
                this.label12.Name = "label12";
                this.label12.Size = new System.Drawing.Size(39, 17);
                this.label12.TabIndex = 24;
                this.label12.Text = "文本5";
                // 
                // label13
                // 
                this.label13.AutoSize = true;
                this.label13.Location = new System.Drawing.Point(722, 464);
                this.label13.Name = "label13";
                this.label13.Size = new System.Drawing.Size(94, 17);
                this.label13.TabIndex = 25;
                this.label13.Text = "QQ:616943421";
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(889, 485);
                this.Controls.Add(this.label13);
                this.Controls.Add(this.label12);
                this.Controls.Add(this.label11);
                this.Controls.Add(this.label10);
                this.Controls.Add(this.label9);
                this.Controls.Add(this.label8);
                this.Controls.Add(this.label7);
                this.Controls.Add(this.textBox10);
                this.Controls.Add(this.textBox9);
                this.Controls.Add(this.textBox8);
                this.Controls.Add(this.textBox7);
                this.Controls.Add(this.textBox6);
                this.Controls.Add(this.textBox5);
                this.Controls.Add(this.label6);
                this.Controls.Add(this.button3);
                this.Controls.Add(this.button2);
                this.Controls.Add(this.label5);
                this.Controls.Add(this.textBox4);
                this.Controls.Add(this.label4);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.textBox3);
                this.Controls.Add(this.textBox2);
                this.Controls.Add(this.textBox1);
                this.Controls.Add(this.label3);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.label1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.TextBox textBox2;
            private System.Windows.Forms.TextBox textBox3;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            private System.Windows.Forms.ImageList imageList1 = new System.Windows.Forms.ImageList();
            private System.Collections.ArrayList imgList = new System.Collections.ArrayList();
            private System.Windows.Forms.Label label4;
            private System.Windows.Forms.TextBox textBox4;
            private System.Windows.Forms.Label label5;
            private System.Windows.Forms.Button button2;
            private System.Windows.Forms.Button button3;
            private System.Windows.Forms.Label label6;
            private System.Windows.Forms.TextBox textBox5;
            private System.Windows.Forms.TextBox textBox6;
            private System.Windows.Forms.TextBox textBox7;
            private System.Windows.Forms.TextBox textBox8;
            private System.Windows.Forms.TextBox textBox9;
            private System.Windows.Forms.TextBox textBox10;
            private System.Windows.Forms.Label label7;
            private System.Windows.Forms.Label label8;
            private System.Windows.Forms.Label label9;
            private System.Windows.Forms.Label label10;
            private System.Windows.Forms.Label label11;
            private System.Windows.Forms.Label label12;
            private System.Windows.Forms.Label label13;
        }
    }
    View Code

    解决方案下载:

    链接: https://pan.baidu.com/s/1r5rGkTzcjNeWJxbRQf1w-Q 提取码: 93xa 复制这段内容后打开百度网盘手机App,操作更方便哦

  • 相关阅读:
    javaBean为什么要实现Serializable接口?
    OpenLayers3的WMS空间查询实现多个图层
    (WPS) 网络地理信息处理服务
    window.open跳过浏览器拦截
    linux改权限
    element-ui MessageBox的bug
    element-ui上传文件带token
    MySQL启动出现The server quit without updating PID file错误解决办法
    重装应用商店
    vscode在vue-cli中按照ESlint自动格式化代码
  • 原文地址:https://www.cnblogs.com/chaguoguo/p/14604811.html
Copyright © 2011-2022 走看看