【问题标题】:Splitting .txt file in c# according to the initial letter in each line根据每行的首字母在c#中拆分.txt文件
【发布时间】:2021-03-26 11:57:11
【问题描述】:

我想将每行一个句子的 .txt 文件拆分为两个 .txt 文件, 在第一个 .txt 文件中,应该只有首字母为 A、B、C、...、N 的句子。 在第二个 .txt 文件中应该只有首字母为 O,P,Q,...,Z 的句子。

我试过了:

    using System;
using System.IO;
using System.Text;

class Program
{


    static void Main(string[] args)
    {
        MyStreamReader sr = new MyStreamReader();
        sr.ReadMyFile();



        MyStreamWriter sw = new MyStreamWriter();
        sw.WriteMyFile();

        MyStreamWriter2 sw2 = new MyStreamWriter2();
        sw2.WriteMyFile2();
    }

    static void Declare()
    { public StringBuilder AtoN;
    public StringBuilder OtoZ; }
    
    
    class MyStreamWriter
    {
        public void WriteMyFile(StringBuilder AtoN)
        {
            StreamWriter sw = null;
            try
            {
                sw = new StreamWriter(@"C:\ProgramkoTest\FirstHalf");
                sw.WriteLine(AtoN);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                    sw.Dispose();
                }
            }
        }
    }
class MyStreamWriter2
{
    public void WriteMyFile2(StringBuilder OtoZ)
    {
        StreamWriter sw2 = null;
        try
        {
            sw2 = new StreamWriter(@"C:\ProgramkoTest\SecondHalf");
            sw2.WriteLine(OtoZ);
        }
        catch (Exception e2)
        {
            Console.WriteLine(e2.Message);
        }
        finally
        {
            if (sw2 != null)
            {
                sw2.Close();
                sw2.Dispose();
            }
        }
    }
}
class MyStreamReader
    {
        public void ReadMyFile(StringBuilder AtoN, StringBuilder OtoZ)
        {
            StreamReader sr = null;
            string line = null;
            try
            {
                string pathToTestFile = @"C:\ProgramkoTest\LinesToSplit.txt";

                sr = new StreamReader(pathToTestFile);
                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine();
                    if (line != null)
                    {
                        Console.WriteLine(line);
                        char linePart0 = line[0];
                        if (linePart0 > 64 && linePart0 < 79)
                        {
                            StringBuilder AtoN = new StringBuilder(line);
                        }
                        else if (linePart0 > 79 && linePart0 < 91)
                        {
                            StringBuilder OtoZ = new StringBuilder(line);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                }
            }
        }
    }
}

我在 unicode 大写字母 A 中搜索了值 65。所以我尝试了每行中第一个字母的值,然后将其放入 StringBuilder。感谢您的帮助。

【问题讨论】:

  • 您应该得到编译时错误,因为您在没有所需的 StringBuilder 参数的情况下调用 sr.ReadMyFile();...
  • 我该如何修复它?
  • ... 使用所需参数调用函数?考虑学习一些 C# 教程以及使用方法和参数进行编程的基础知识。
  • 当我这样做时,它只会显示更多错误......
  • "显示更多错误" 那么,它之前显示过错误吗?请尽可能完整地回答您的问题,并提供正在发生的任何错误的文本。请参阅How to Ask 了解更多信息。

标签: c# streamreader streamwriter


【解决方案1】:

您也可以使用它来节省一些内存,因为内存中只保留了一行。也可能更快:

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (StreamReader sr = new StreamReader("testfile.txt"))
            {
                using (StreamWriter aToNWriter = new StreamWriter("outputA-N.txt"))
                {
                    using (StreamWriter oToZWriter = new StreamWriter("outputO-Z.txt"))
                    {
                        string line = null;
                        while ((line = sr.ReadLine()) != null)
                        {
                            if (string.IsNullOrEmpty(line))
                                continue;

                            char firstCharInLine = line[0];

                            if (firstCharInLine >= 'A' && firstCharInLine <= 'N')
                            {
                                aToNWriter.WriteLine(line);
                            }
                            else
                            {
                                oToZWriter.WriteLine(line);
                            }
                        }
                    }
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    基本逻辑应该是这样的(如果我没有误解你的要求的话):

    using System.IO;
    using System.Text;
    
    namespace MyDemoApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                StringBuilder builderA = new StringBuilder();
                StringBuilder builderB = new StringBuilder();
    
                foreach (string line in File.ReadAllLines(@"e:\MyInputFile.txt"))
                {
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        char firstChar = line.ToUpperInvariant()[0];
    
                        if (firstChar >= 'A' && firstChar <= 'N')
                        {
                            builderA.AppendLine(line);
                        }
                        else if (firstChar >= 'O' && firstChar <= 'Z')
                        {
                            builderB.AppendLine(line);
                        }
                        else
                        {
                            // TODO: numbers, special characters ...
                        }
                    }
                }
    
                File.WriteAllText(@"e:\\fileA.txt", builderA.ToString());
                File.WriteAllText(@"e:\\fileB.txt", builderB.ToString());
    
            }
        }
    }
    

    当然,还有优化的空间。但基本上它会按照您的描述进行。

    【讨论】:

    • 谢谢,我想请问在哪里可以阅读有关文件的内容。 ...您正在使用。
    • 我是否需要类似的东西:使用 System.File; ?
    • @David395,使用 System.IO(文件所在的位置)
    • 是的,谢谢我很愚蠢,这与我用于 StreamRider 和 StreamWriter 的方式相同...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多