读取平面文件,分组并写入文件(在空白处添加特殊字符作为'*')

问题描述 投票:-1回答:2
E2739158012008-10-01O9918107NPF7547379999010012008-10-0100125000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
E2739158PU0000-00-00                     010012008-10-0100081625219                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
E3180826011985-01-14L9918007NPM4927359999010011985-01-1400005620000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
E3180826PU0000-00-00                     020011985-01-14000110443500021997-01-1400000518799                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
E3292015011985-01-16L9918007NPM4927349999010011985-01-1600003623300   

我有这个平面文件,我需要根据第2位到第8位将其分组example(2739158/3180826/3292015)并写入另一个平面文件。因此,以“ E”开头的数据应与该组字段一起在单行中重复(起始位置为第2至第8位),我应在“ E”之后占据第9位。

此外,我还需要用('*'星号)替换空白空间例如

第一行

2739158**E**012008-10-01O9918107NPF7547379999010012008-10-0100125000000*****E**012008-10-01O9918107NPF7547379999010012008-10-0100125000000  

第二行

3180826**E**011985-01-14L9918007NPM4927359999010011985-01-1400005620000**E**011985-01-14L9918007NPM4927359999010011985-01-140000562000**E**011985-01-14L9918007NPM4927359999010011985-01-140000562000***

第三行

3292015**E**011985-01-16L9918007NPM4927349999010011985-01-1600003623300****   

我们可以在Stream reader c#中做到这一点吗?任何帮助将不胜感激。文件大小超过285 MB,因此最好通过Stream Reader进行阅读?谢谢

c# file stream streamwriter reader
2个回答
0
投票

@@ jdweng:非常感谢您的输入。我以某种方式尝试了而不进行分组,它按预期工作。感谢尝试解决此问题的每个人。

                                                                                                        string sTest= string.Empty;                                                                                                            List<SortLines> lines = new List<SortLines>();                
            List<String> FinalLines = new List<String>();
            using (StreamReader sr = new StreamReader(@"C:\data\Input1.txt))
            {                  
                    sr.ReadLine();

                string line = "";
                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine();
                    //line = line.Trim();
                    if (line.Length > 0)
                    {
                        line = line.Replace(" ", "*");

                        SortLines newLine = new SortLines()
                        {
                            key = line.Substring(1, 7),
                            line = line
                        };                          
                        if (sTest != newLine.key)
                        {
                            //Add the Line Items to String List
                            sOuterLine = sTest + sOneLine;
                            FinalLines.Add(sOuterLine);
                            string sFinalLine = newLine.line.Remove(1, 7);
                            string snewLine = newLine.key + sFinalLine;
                            sTest = snewLine.Substring(0, 7);
                            //To hold the data for the 1st occurence
                            sOtherLine = snewLine.Remove(0, 7);
                            bOtherLine = true;
                            string sKey = newLine.key;
                            lines.Add(newLine);

                        }
                        else if (sTest == newLine.key)
                        {

                            string sConcatLine = String.Empty;
                            string sFinalLine = newLine.line.Remove(1, 7);
                            //Check if 1st Set
                            if (bOtherLine == true)
                            {
                                sOneLine = sOtherLine + sFinalLine;
                                bOtherLine = false;
                            }
                        //If not add subsequent data
                            else
                            {
                                sOneLine = sOneLine + sFinalLine;
                            }
                            //Check for the last line in the flat file
                            if (sr.Peek() == -1)
                            {
                                sOuterLine = sTest + sOneLine;
                                FinalLines.Add(sOuterLine);
                            }                                                             
                        }

                    }

                }
            }
            //Remove the Empty List
            FinalLines.RemoveAll(x => x == "");              
            StreamWriter srWriter = new StreamWriter(@"C:\data\test.txt);

            foreach (var group in FinalLines)
            {
                srWriter.WriteLine(group);                    
            }
            srWriter.Flush();
            srWriter.Close();              

-1
投票

尝试下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
         const string INPUT_FILENAME = @"c:\temp\test.txt";
         const string OUTPUT_FILENAME = @"c:\temp\test1.txt";
         static void Main(string[] args)
         {
             List<SortLines> lines = new List<SortLines>();
             StreamReader reader = new StreamReader(INPUT_FILENAME);
             string line = "";
             while ((line = reader.ReadLine()) != null)
             {
                 line = line.Trim();
                 if (line.Length > 0)
                 {
                     line = line.Replace(" ", "*");

                     SortLines newLine = new SortLines() { key = line.Substring(2, 7), line = line };
                     lines.Add(newLine);
                 }
             }
             reader.Close();
             var groups = lines.GroupBy(x => x.key);
             StreamWriter writer = new StreamWriter(OUTPUT_FILENAME);

             foreach (var group in groups)
             {
                 foreach (SortLines sortLine in group)
                 {
                     writer.WriteLine(sortLine.line);
                 }
             }
             writer.Flush();
             writer.Close();

         }
    }
    public class SortLines : IComparable<SortLines>
    {
        public string line { get; set; }
        public string key { get; set; }
        public int CompareTo(SortLines other)
        {
            return key.CompareTo(other);
        }
    }



}
© www.soinside.com 2019 - 2024. All rights reserved.