将文本文件转换为数组

时间:2015-05-26 15:50:45

标签: c#

所以这是我项目的总体要点: 我有三个文件:file1,file2和file3。这些文件里面有很多电子邮件。 file1应该包含所有电子邮件,其中file2有一个集合,file3有另一个集合。我需要编写一个程序来读取这些文件,看看这三封电子邮件中缺少哪些电子邮件,然后写入一个名为master文件的新文件,主文件的输出必须告诉我每个文件丢失了哪些电子邮件文件。

例如,假设Anna在file2中,但在file1中找不到Anna。主文件的输出应该告诉我,我需要将Anna添加到file1(我知道Anna不是电子邮件,但这只是一个例子。)

下面我有我一直在处理的代码,并且我有一个用于查找和/或编写文本文件的代码。我的问题是我如何获取这些文件并将它们放入一个数组中,以便我可以对它们进行排序并获得我想要的输出。或者如果不是这样你怎么做,我必须采取什么方式。

    class Program
    {
const string file1 = "file1.txt";
const string file2 = "file2.txt";
const string file3 = "file3.txt";

static void Main(string[] args)
{
            try
    {
     var count = File.ReadLines(file1).Count();
        //This code simply checks if the full_pack file exists and if it does, it will give how many lines are in the file
        if (!File.Exists(file1))
        {
            Console.WriteLine("File Not Found");
        }

        else
        {
            string[] lines = System.IO.File.ReadAllLines(file1);
            Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file1));
            Console.WriteLine(lines.Count());

        }

        //this code is similar to the one above, however it is for den1
        if (!File.Exists(file2))
        {
            Console.WriteLine("File Not Found");
        }
        else
        {
            string[] lines = System.IO.File.ReadAllLines(file1);
            Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file2));
            Console.WriteLine(lines.Count());
        }

        //this code is similar to the one above, however it is for den2
        if (!File.Exists(file3))
        {
            Console.WriteLine("File Not Found");
        }
        else
        {
            string[] lines = System.IO.File.ReadAllLines(file3);
            Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file3));
            Console.WriteLine(lines.Count());
        }

        //This code is to create a new file called masterfile
        string masterFile = @"C:\Users\povermyer\documents\visual studio 2013\Projects\BoyscoutProject2\BoyscoutProject2\Data\masterFile.txt";
        if (!File.Exists(masterFile))
        {
            File.Create(masterFile);
            TextWriter tw = new StreamWriter(masterFile);
            tw.WriteLine("Here is the list of missing emails");
            tw.Close();
        }
        else if (File.Exists(masterFile))
        {
            TextWriter tw = new StreamWriter(masterFile);
            tw.WriteLine("Here is the list of missing emails");
            tw.Close();
        }

    }

    catch (Exception ex)
    {

        Console.WriteLine(ex.ToString());
    }
    finally
    {
        Console.ReadKey();
    }

3 个答案:

答案 0 :(得分:0)

根据我对你的问题的理解,我在这里编写了这段代码而没有经过测试。希望它会给你一些想法。

string[] file1Content = System.IO.File.ReadAllLines("file1.txt");
string[] file2Content = System.IO.File.ReadAllLines("file2.txt");
string[] file3Content = System.IO.File.ReadAllLines("file3.txt");
var file2Missings = file2Content.Except(file1Content);
var file3Missings = file3Content.Except(file1Content);
string[] MasterFileContent = file2Missings.Concat(file3Missings).Distinct().ToArray();

foreach(string s in MasterFileContent.Distinct().ToArray())
      Console.WriteLine(s + " must be placed in file 1"); 

<强>更新

您可以在此处看到它:http://goo.gl/3fP56T

答案 1 :(得分:0)

您可以使用以下代码: 的被修改

        //Check if files are available
        var file1 = File.ReadAllLines("file1.txt");
        var file2 = File.ReadAllLines("file2.txt");
        var file3 = File.ReadAllLines("file3.txt");
        var result = "result.txt";
        var ThreeAndTwo = file3.Concat(file2);
        foreach (var email in ThreeAndTwo)
        {
            if (!file1.Contains(email))
                File.AppendAllText(result, email + " needs to be placed in file1" + Environment.NewLine);
        }

编辑2 演示结果

file1.txt的内容

  

TEST1

     

TEST2

     

TEST3

     

TEST4

     

TEST5

file2.txt的内容

  

TEST2

     

test22

     

test22

file3.txt的内容

  

TEST3

     

test33

     

test333

result.txt的内容

  

test22需要放在file1

中      

test222需要放在file1

中      

test33需要放在file1

中      

test333需要放在file1

答案 2 :(得分:0)

HadiRj使用HashSet有一个好点,但逻辑是错误的。你需要从文件2和3中删除当前在文件1中的所有记录,因此保留在文件2和3中的记录首先是file1中没有的记录:

string[] file1 = System.IO.File.ReadAllLines("file1.txt");
string[] file2 = System.IO.File.ReadAllLines("file2.txt");
string[] file3 = System.IO.File.ReadAllLines("file3.txt");

HashSet<string> missingFromTwo = new HashSet<string>(file2);
HashSet<string> missingFromThree = new HashSet<string>(file3);

//this line will remove all etries in file1 from file2,
//hence file2 will remain with entries that do not exist
//in file one.
missingFromTwo.ExceptWith(file1);

//this line will do the same with file3.
missingFromThree.ExceptWith(file1);

StringBuilder FinalResult = new StringBuilder();
FinalResult.AppendLine("emails from file 2 that need to be added to file1:");
FinalResult.Append(String.Join(Environment.NewLine, missingFromTwo.ToArray()));


FinalResult.AppendLine("emails from file 3 that need to be added to file1:");
FinalResult.Append(String.Join(Environment.NewLine, missingFromThree.ToArray()));

System.IO.File.WriteAllText("Master.txt", FinalResult.ToString());