文件检查和打印

时间:2011-09-15 21:13:41

标签: c# file-io

我正在编写一个程序,一次打开一个文件,对其进行解析,然后将解析后的数据输出到一个新的.txt文档中,该文档根据文件进入而命名。有超过50个文件正在运行被阅读和解析。

因此,如果已打开的文件的名称如下:STACK-OVERFLOW-125663-D2.txt,则输出文件将类似于此125663-D2.txt

每次读取文件时,都会解析其部件号。每个文件将包含与此类似的行(第8个分隔的逗号值(即119082,119083,119040,119085,119084)是部件号值。)

"00003",6,"D","C20",-70.10,42.06,90.00,"119082",0,1,2,0,0,"",0,"001"
"00004",6,"D","C21",-67.91,42.06,90.00,"119082",0,1,2,0,0,"",0,"001"
"00005",13,"D","C23",-66.91,59.07,180.00,"119083",0,1,2,0,0,"",0,"002"
"00006",13,"D","R10",-77.32,66.88,90.00,"119040",0,1,2,0,0,"",0,"003"
"00007",13,"D","L3",-77.64,77.48,90.00,"119085",0,1,2,0,0,"",0,"004"
"00008",20,"D","D1",-62.91,103.77,0.00,"119084",0,1,2,0,0,"",0,"005"
"00009",21,"D","D1",-25.83,103.77,0.00,"119084",0,1,2,0,0,"",0,"005"
"00010",14,"D","L3",-40.56,77.48,90.00,"119085",0,1,2,0,0,"",0,"004"
"00011",14,"D","R10",-40.24,66.88,90.00,"119040",0,1,2,0,0,"",0,"003"

现在我需要做的是检查另一个.txt文件..让我们说它被称为“DATABASE.txt”,看看这些部件号是否已存在。此数据库文件如下所示:

119082:    125663-D2, 123456-A1,
119083:    125663-D2,
119085:    125663-D2, 123456-A1, 987654321-Z11234, 1111111-B50

因此,在DATABASE.txt文件和上面打开的文件中,我想检查打开文件中的所有部件号,看看它们是否存在于数据库中。

  • 如果该部件确实存在,我想将文件名(输出文件)连接到找到部件号的行的末尾。

  • 如果该部件不存在,我想将该部件添加到文件中并使用list.Sort()对文件进行排序。

我不确定如何做到这一点,任何人都可以帮忙吗?


以下是我目前的一些代码:

List<string> partNumberLines = new List<string>();
string file = openFile.FileName;
string splitFile = file.Split('\\');
string[] savedName = splitFile[splitFile.Length - 1].Split('.');
string[] lineNumber = savedNamed[2].Split('-');
string fileName = savedNamed[1] + "-" + lineNumber[0] + ".txt";

foreach (string line in fileList)
{
    string[] splitLine = line.Split('\n');
    for (int i = 0; i < splitLine.Length; i++)
    {
        string tempSplit = splitLine[i].Split(',');  // splits each line by commas
        if (tempSplit.Length.Equals(16))
        {
            tempSplit[7] = tempSplit[7].TrimStart('"');  //trims the quotes from the part numbers
            tempSplit[7] = tempSplit[7].TrimEnd('"');
        }
    }
}
partNumberLines = partNumberLines.Distinct().ToList();  //gets rid of duplicate partnumbers in one file.

所以我的代码获取了所有的部件号和文件的名称..我只是不知道如何打开现有文件(如果它不存在,创建它)并搜索文件和在列表 partNumberLines中查找匹配项。如果匹配,则将文件名连接到当前行。如果不匹配,请创建一个新行并添加部件号和文件名,然后按部件号以数字方式对文件进行排序。

任何人都可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

希望你没有放弃。这是一个示例类。我从上一篇文章中完成了它。将上面的数据库数据保存到database.txt,将您的零件数据保存到parts.txt,然后修改路径以查看其工作原理。希望它能帮到你。如果您还有其他问题,请随时提出。

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

namespace ConsoleApplication1
{
    class Program
    {
        private class DataBaseRecord
        {
            public string PartNumber { get; set; }
            public List<string> FileNames { get; set; }
            public DataBaseRecord(string _PartNumber, List<string> _FileNames)
            {
                PartNumber = _PartNumber;
                FileNames = _FileNames;
            }
        }

        private class DataBase
        {
            public string databaseFile { get; set; }
            List<DataBaseRecord> records;
            public DataBase(string _databaseFile)
            {
                databaseFile = _databaseFile;
                records = new List<DataBaseRecord>();
            }
            public void AddRecord(string partNumber, string fileName)
            {
                if (string.IsNullOrWhiteSpace(partNumber))
                    return;

                if (string.IsNullOrWhiteSpace(fileName))
                    return;

                bool exists = records.Count(x => x.PartNumber == partNumber) > 0;
                if (!exists)
                {
                    records.Add(new DataBaseRecord(partNumber, new List<string>() { fileName }));
                }
                else
                {
                    var record = from x in records where x.PartNumber == partNumber select x;
                    foreach (DataBaseRecord dbr in record)
                    {
                        exists = dbr.FileNames.Count(x => x == fileName) > 0;
                        if (!exists)
                            dbr.FileNames.Add(fileName);
                    }
                }
            }
            public void Read()
            {
                // read all database records into data structure
                using (StreamReader sr = new StreamReader(databaseFile))
                {
                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        string partNumber = line.Split(':')[0].Trim();
                        if (partNumber[0] == '\"')
                            partNumber = partNumber.Substring(1, partNumber.Length - 2);
                        string[] files = line.Split(new string[]{":"}, StringSplitOptions.None)[1].Split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries);
                        List<string> fileNumbers = new List<string>();
                        foreach (String file in files)
                        {
                            if (!string.IsNullOrWhiteSpace(file))
                            {
                                fileNumbers.Add(file.Trim());
                            }
                        }
                        records.Add(new DataBaseRecord(partNumber, fileNumbers));
                    }
                }
            }
            public void Write()
            {
                // write out database using the records
                var sortedRecords = from x in records orderby x.PartNumber select x;
                using (StreamWriter sw = new StreamWriter(databaseFile))
                {
                    foreach (DataBaseRecord record in sortedRecords)
                    {
                        string line = record.PartNumber + ": ";
                        for (int index = 0; index < record.FileNames.Count; index++)
                        {
                            line += record.FileNames[index];
                            if (index < record.FileNames.Count - 1)
                                line += ", ";
                        }
                        sw.WriteLine(line);
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            // replace with name of your database
            DataBase db = new DataBase(@"C:\Users\jondoe\Desktop\DataBase.txt");
            db.Read();

            // replace with list of your parts files
            string[] partsFiles = new string[] { @"C:\Users\jondoe\Desktop\parts.txt" };
            foreach (string partsFile in partsFiles)
            {
                using (StreamReader sr = new StreamReader(partsFile))
                {
                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        string partNumber = line.Split(new string[] { "," }, StringSplitOptions.None)[7];
                        if (partNumber[0] == '\"')
                            partNumber = partNumber.Substring(1, partNumber.Length - 2);
                        db.AddRecord(partNumber, Path.GetFileNameWithoutExtension(partsFile));
                    }
                }                
            }

            db.Write();
        }
    }
}

修改

如果您想要一个静态数据库并且想要允许用户选择零件文件,那么您可以在按钮单击事件中执行此操作:

private void btnOpenFile_Click(object sender, EventArgs e)
{
    DataBase db = new DataBase(@"C:\Users\JonDoe\Desktop\DataBase.txt");
    db.Read();

    using (OpenFileDialog ofd = new OpenFileDialog())
    {
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            using (StreamReader sr = new StreamReader(ofd.FileName))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string partNumber = line.Split(new string[] { "," }, StringSplitOptions.None)[7];
                    if (partNumber[0] == '\"')
                        partNumber = partNumber.Substring(1, partNumber.Length - 2);
                    db.AddRecord(partNumber, Path.GetFileNameWithoutExtension(ofd.FileName));
                }
            }
            db.Write();
        }
    }
}

答案 1 :(得分:1)

请勿自行解析CSV。使用FileHelpers库。可能存在很多异常,FileHelpers可以很好地处理这些异常。

其次,你正在做足够的数据操作是一个简单的数据库可能会有所帮助。也许是SQL Express,或单个文件嵌入式数据库(SQL Server Compact,SQLite)。

最后要手动执行此操作,您只需在内存中构建表。您真正拥有的是部件号和文件之间的多对多关系。所以你有两个表和一个连接表。

现在,因为“filename”在您的示例中只有一个属性( filename ),所以它可以作为附加列附加在连接表中。所以你有2张桌子。第二个看起来像

JoinTable
-------------------
PartNum | Varchar
Filename| Varchar

你已经拥有的第一张表。

因此,如果您使用List<List<string>>在内存中复制这两个表,您应该可以使用LINQ完成此操作而不会遇到任何问题。虽然我个人会建立新的类或至少结构来代表两个表元组。

希望这有帮助。

答案 2 :(得分:0)

这里有一个快速的运行..它绝对可以使用改进,但它是一个开始:)

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

namespace ConsoleApplication2
{
    public class FileIo
    {
        private Dictionary<int, CommaDelimitedStringCollection> dataBase;
        private const string DATABASE_PATH = @"D:\Temp\Test\DATABASE.txt";

        public void Run()
        {
            var files = new List<string> { @"D:\Temp\Test\Test1.txt", @"D:\Temp\Test\Test2.txt", @"D:\Temp\Test\Test3.txt" };

            dataBase = GetDatabase();

            foreach (var file in files)
            {
                Search(file, GetPartNumbers(file));  
            }

            WriteFileContents(DATABASE_PATH, BuildPartsDbStr());
        }

        private void Search(string fileName, List<int> partNums)
        {
            foreach (var partNum in partNums)
            {
                var path = Path.GetFileNameWithoutExtension(fileName);
                if (dataBase.Keys.Contains(partNum))
                {
                    dataBase[partNum].Add(path);
                }
                else
                {
                    dataBase.Add(partNum, new CommaDelimitedStringCollection { path });
                }
            }
        }

        private string BuildPartsDbStr()
        {
            var sb = new StringBuilder();
            var db  = from x in dataBase orderby x.Key select x;

            foreach (var record in db)
            {
                sb.AppendLine(string.Format("{0}: {1}", record.Key, record.Value));
            }

            return sb.ToString();
        }

        private Dictionary<int, CommaDelimitedStringCollection> GetDatabase()
        {
            var contents = GetFileContents(DATABASE_PATH);
            var commaStr = new CommaDelimitedStringCollection();
            var db = new Dictionary<int, CommaDelimitedStringCollection>();
            var id = 0;
            var collection = new CommaDelimitedStringCollection();

            for (var i = 0; i < commaStr.Count; i++ )
            {
                var str = commaStr[i];
                if (str.Contains(":"))
                {
                    collection.Add(str.Split(':')[1]);
                    if (i > 0) db.Add(id, collection);
                    collection = new CommaDelimitedStringCollection();
                    id = int.Parse(str.Split(':')[0]);
                }
                else
                {
                    collection.Add(str);
                }
            }

            return db;
        }

        private List<int> GetPartNumbers(string filePath)
        {
            var contents = GetFileContents(filePath);
            var commaStr = new CommaDelimitedStringCollection();
            var result = new List<int>();                

            commaStr.AddRange(contents.Split(','));

            for(var i = 7; i < commaStr.Count; i += 15)
            {
                result.Add(int.Parse(commaStr[i].Replace("\"", string.Empty)));
            }

            return result;
        } 


        private string GetFileContents(string path)
        {
            using (var stream = new StreamReader(path))
            {
                //yes I know this is evil - I don't have all night :P
                return stream.ReadToEnd();
            }
        }

        private void WriteFileContents(string path, string contents)
        {
            using (var stream = new StreamWriter(path, false))
            {
                stream.Write(contents);
                stream.Flush();
            }
        }

    }
}