C#windows桌面应用小程序制作——大文件数据分段解析存储

现在的任务就是做一个大文件解析的桌面应用小程序,具体需求就是:将一个很大的文件里的数据按一定标志拆分然后分别保存到某个文件夹下面。

解析的文件内容为以下内容:

windows 应用小程序界面

 

    具体代码主要分为两步:

   

 private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();//创建打开文件夹对话框对象
            //op.ShowDialog(); //实现对话框展示
            //op.Filter = "fasta文件|*.fasta";//设置文件后缀过滤
            if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                filePath = op.FileName;     //获取所选文件的全路径
                nowFilePath=filePath;
                //TextBox1.Text = File.ReadAllText(filePath);  //通过textbox1
            }
        }

 第一个按钮主要是进行打开文件选择文件的操作;

 private void button2_Click(object sender, EventArgs e)
        {
            string totalContent = "";
            int i = 1;
            string directoryPath = @"D:/xyyFile3/";
            if (!Directory.Exists(directoryPath))//如果路径不存在
            {
                Directory.CreateDirectory(directoryPath);//创建一个路径的文件夹
            }
            var reader = new StreamReader(nowFilePath);
            var content = reader.ReadLine();//读一行
            totalContent = content;
              if(content.Substring(0, 1) ==">")//判断开头是否为>
            {
                 content = reader.ReadLine();//是,继续读取一行
                 totalContent += content;
                while (content.Substring(0, 1)!=">"&& content != null) {
                    content = reader.ReadLine();
                    if (content == null)
                    {
                        FileStream fs1 = new FileStream("D:\\xyyFile3\\" + i + ".txt", FileMode.Create, FileAccess.Write);//创建写入文件
                        StreamWriter sw = new StreamWriter(fs1);
                        //totalContent = totalContent.Replace(")", ")" + "\r\n");
                        sw.WriteLine(totalContent);
                        sw.Close();
                        fs1.Close();
                        break;
                    }
                    else {
                        
                    }
                    
                    while(content.Substring(0, 1) == ">")
                    {
                       // totalContent = content;
                        if (!File.Exists("D:\\xyyFile3"))
                        {
                            FileStream fs1 = new FileStream("D:\\xyyFile3\\" + i + ".txt", FileMode.Create, FileAccess.Write);//创建写入文件
                            StreamWriter sw = new StreamWriter(fs1);
                            //totalContent = totalContent.Replace(")", ")" + "\r\n");
                            sw.WriteLine(totalContent);
                            sw.Close();
                            fs1.Close();
                        }
                        else
                        {
                            FileStream fs = new FileStream("D:\\xyyFile2\\" + i + ".txt", FileMode.Open, FileAccess.Write);
                            StreamWriter sr = new StreamWriter(fs);
                            sr.WriteLine(totalContent);//开始写入值
                            sr.Close();
                            fs.Close();
                        }
                        i++;
                        totalContent = content;
                        content = reader.ReadLine();
                    }
                    totalContent += content;
                }
               

            }
            MessageBox.Show("解析完成,默认保存在D://xyyFile2中");
        }

  第二个按钮就是进行文件内容的解析。

 

posted @ 2017-09-27 09:30  划过天际  阅读(1721)  评论(0编辑  收藏  举报