qq_50918463 2021-03-30 21:07 采纳率: 100%
浏览 1021
已采纳

C# 读取时如何跳过txt文件第一行

 

目前在做毕设,非计算机专业,有一点C语言基础。写一个软件处理txt文件里面的数据,用的streamreader读取文件数据,txt文件比较多,用streamwriter创建了一个合并所有txt文件的新txt文件,现在有个问题是我不要合并的每个txt文件的第一行,也就是stage0-11那一行,请问这个要怎么处理?

 private void button4_Click(object sender, RoutedEventArgs e)//打开文件窗口后台代码
        {

            OpenFileDialog openFileDialog = new OpenFileDialog();//打开文件
            openFileDialog.Title = "选择文件";
            openFileDialog.Multiselect = true;
            openFileDialog.Filter = "文本文件|*.txt";
            if (openFileDialog.ShowDialog() == true)
            {

                path = openFileDialog.FileNames;

                for (int i = 0; i < path.Length; i++)
                {
                    FileStream Read = new FileStream(path[i], FileMode.Open, FileAccess.Read);

                    StreamReader sr = new StreamReader(openFileDialog.FileName, Encoding.Default);
                    textbox1.Text += sr.ReadToEnd();//将读取内容显示到textbox

                    sr.Close();
                }
            }

            using (StreamWriter writer = new StreamWriter(System.Environment.CurrentDirectory + path))
            {
                string filenew = "test.txt";
                FileStream fw = null;
                StreamWriter sw = null;
                fw = File.Create(filenew);
                sw = new StreamWriter(fw);
                for (int i = 0; i < path.Length; i++)
                {
                    using (StreamReader reader = File.OpenText(path[i]))
                    {
                        sw.Write(reader.ReadToEnd());//合并所有选中的.txt*文档;文件地址:C:\Users\lxd\source\repos\data processing software\data processing software\bin\Debug\test.txt
                    }
                }
            }

        }
  • 写回答

1条回答 默认 最新

  • 爱晚乏客游 2021-03-30 21:22
    关注

    c#不是很熟,但是应该可以像C++那样,随便用个变量接受下ReadLine(),这就可以跳过一行,然后下面一行就是你要的东西了。

    比如:

    string str = streamReader.ReadLine();//这行就是第一行的内容
    while (streamReader.Peek() != -1)
    
    {
      string ss = streamReader.ReadLine();
      Console.WriteLine(ss)
    
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加