从其他两个文件创建文件

时间:2013-01-03 00:34:57

标签: c# text streamwriter compareto

到目前为止我有这样的代码:

private void button3_Click(object sender, EventArgs e)
{
    label1.Visible = true;
    label2.Visible = true;
    var items = File.ReadAllLines("updated database folder/Resellers_Inventory.txt").
    Skip(1).
    Select(s =>
    {
        var strings = s.Split('|');
        if (strings.Length != 2) throw new FormatException();
        return new { Code = strings[0], Stock = Convert.ToInt32(strings[1]) };
    }).
    ToArray();

    var compareTo = File.ReadAllLines("old database folder/ProductAll.txt").
                Skip(1).
                Select(s =>
                {
                    var strings = s.Split(',');
                    if (strings.Length != 6) throw new FormatException();
                    return new { Path = strings[2].Trim('"'), Id = Convert.ToInt32(strings[3]), Name = strings[4].Trim('"'), Code = strings[5].Trim('"'), Stock = Convert.ToInt32(strings[6]) };
                }).
                ToArray();

    string foldername = DateTime.Now.ToString("yyyy--MM--dd");
    Directory.CreateDirectory("Update file of " + foldername);
    string UpdeitoFailas = @"Update file of " + foldername + "/update.txt";

    foreach (var item in items.Where(i => compareTo.Any(i2 => i.Code == i2.Code)))
    {
        if ((File.Exists(UpdeitoFailas)) && i.Stock != i2.Stock)
        {
            using (StreamWriter sw = new StreamWriter(UpdeitoFailas))
            {
                sw.Write("Product, " + i2.Path + ", " + i2.Id + ", " + i2.Name + ", " + i2.Code + ", " + i.Stock);
             }
         }
     }

文件“Resellers_Inventory.txt”是这样的(包含数十万行):

Order Code|Stock
ACREPAIR|1031
AF813|18
AF823|12
AFCOB11|21
AFCS300|33
AFCS3000|1
AFEM4|5
AFOMNI|17
AFOX2|-3
AFOX3|-3
AFROD|28
AFSENSOR|50
AFUF21|24
AN00001|-1
AN00002|21
AN00003|4
AN00004|4
AN00005|9
...

ProductAll.txt文件还包含数十万行,如下所示:

Action,CategoryPath,ID,Name,Code,Stock
"Product","Home > Opto-electronics > LED > Standard LED, Multicolour",2226,"KINGBRIGHT LED, 3MM, HE-RED/GRN L-93WEGW","SC07621",202
"Product","Home > Resistors > Fixed",2228,"VISHAY DRALORIC RESISTOR, 0402, 5%, 10K0 CRCW040210K0JNEAIF","RE06211",0
"Product","Home > Resistors > Fixed",2229,"VISHAY DRALORIC RESISTOR, 0402, 5%, 3R90 CRCW04023R90JNEAIF","RE06212",0
"Product","Home > Resistors > Fixed",2230,"VISHAY DRALORIC RESISTOR, 0402, 5%, 2R70 CRCW04022R70JNEAIF","RE06220",25
"Product","Home > Resistors > Fixed",2231,"VISHAY DRALORIC RESISTOR, 0402, 5%, 33R0 CRCW040233R0JNEAIF","RE06221",0
"Product","Home > Resistors > Fixed",2232,"VISHAY DRALORIC RESISTOR, 0402, 5%, 100R CRCW0402100RJNEAIF","RE06226",0
"Product","Home > IC's > Comparators",2234,"STMICROELECTRONICS IC, COMPARATOR DUAL, DIP8, 393 LM393N","SC10207",57
"Product","Home > IC's > Amplifiers > Operational",2237,"STMICROELECTRONICS OP AMP, QUAD JFET, DIP14 TL084CN","SC07929",82
"Product","Home > IC's > Amplifiers > Audio Power",2239,"NATIONAL SEMICONDUCTOR AMP, AUDIO 0.25W, DIP8, 386 LM386N-1","SC08430",83
"Product","Home > IC's > Microcontrollers",2241,"MICROCHIP 8BIT FLASH MCU, 12F675, DIP8 PIC12F675-I/P","ACREPAIR",16
...

基本上我想要创建的第三个文件看起来像ProductAll.txt,但是会有不同之处:如果在Resellers_Inventory.txt中库存与ProductAll.txt不同,那么我们写一行来更新。 txt与ProductAll.txt中的行相同,但具有来自Resellers_Inventory.txt的库存值。如果股票价值相同,我们会跳过此行,不要写入新的第三个文件。匹配行(股票)通过匹配两个文件中相同的项目代码来实现。

目前我遇到了这样的错误:

Error   1   The name 'i' does not exist in the current context  C:\Users\Tutis\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 90  53  WindowsFormsApplication1
Error   2   The name 'i2' does not exist in the current context C:\Users\Tutis\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 90  64  WindowsFormsApplication1
Error   3   The name 'i2' does not exist in the current context C:\Users\Tutis\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 94  48  WindowsFormsApplication1
Error   4   The name 'i2' does not exist in the current context C:\Users\Tutis\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 94  65  WindowsFormsApplication1
Error   5   The name 'i2' does not exist in the current context C:\Users\Tutis\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 94  80  WindowsFormsApplication1
Error   6   The name 'i2' does not exist in the current context C:\Users\Tutis\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 94  97  WindowsFormsApplication1
Error   7   The name 'i' does not exist in the current context  C:\Users\Tutis\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 94  114 WindowsFormsApplication1

但即使没有这个,我也不知道如何写新的update.txt文件符号这些行与ProductAll.txt中的行相同,只是与其他股票价值相同。

2 个答案:

答案 0 :(得分:0)

有点难以看到代码中的内容,但您可以使用File.WriteAllLines来创建结果文件。

我昨天使用你的问题答案显示它的工作,因为我无法得到你发布的工作示例。

    private void UpdateProducts()
    {
        ThreadPool.QueueUserWorkItem((o) =>
        {
            DateTime start = DateTime.Now;
            List<string> updatedProducts = new List<string>();
            List<string[]> stock = new List<string[]>(File.ReadAllLines("G:\\Stock.txt").Select(line => line.Split('|')));
            List<string> products = new List<string>(File.ReadAllLines("G:\\Products.txt"));
            updatedProducts.Add(products[0]);
            SetProgress(products.Count);
            foreach (var item in products)
            {
                if (stock.Any(s => item.Contains(s[0]) && !item.EndsWith(s[1])))
                {
                    string[] p = item.Split(',');
                    int productStockIndex = Array.IndexOf(p, p.Last());
                    int productNameIndex = productStockIndex - 1;
                    string productName = p[productNameIndex].Replace("\"", "");
                    p[productStockIndex] = stock.First(stk => stk[0] == productName)[1];
                    updatedProducts.Add(string.Join(",", p));
                }
                UpdateProgress(updatedProducts.Count);
            }
            File.WriteAllLines(@"G:\Results.txt", updatedProducts.ToArray());
        });
    }

    private void SetProgress(int maxValue)
    {
        base.Invoke((Action)delegate 
        {
            progressBar1.Maximum = maxValue;
        });
    }

    private void UpdateProgress(int value)
    {
        base.Invoke((Action)delegate
        {
            progressBar1.Value = value;
        });
    }

    private void button1_Click(object sender, EventArgs e)
    {
        UpdateProducts();
    }
祝你好运:)

答案 1 :(得分:0)

它似乎不适用于“ProductAll.txt”

中的数据

您需要将CSV中的行分隔为逗号作为分隔符,但 KEEP 用双引号之间的逗号分隔。

String.Split()不是这项工作的好选择。您可以使用this免费库来读取CSV文件,而不是尝试重新发明轮子。

要使用它,唯一需要做的就是添加对LumenWorks.Framework.IO.dll的引用

以下是一个示例控制台项目,使用“快速CSV阅读器”库和字典执行您想要的操作,以加快您在代码字段。

using LumenWorks.Framework.IO.Csv;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace TestProgram
{
    class Product
    {
        public string Action { get; set; }
        public string CategoryPath { get; set; }
        public int ID { get; set; }
        public string Name { get; set; }
        public string Code { get; set; }
        public int Stock { get; set; }
    }

    static class Extensions
    {
        public static string DoubleQuoteString(this string str)
        {
            return '"' + str + '"';
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var resellersInventoryQuery = from lineTokens in ReadCsvLines("data\\Resellers_Inventory.txt", true, '|')
                                          select new { Code = lineTokens[0], Stock = Convert.ToInt32(lineTokens[1]) };

            Dictionary<string, int> orderStock = resellersInventoryQuery.ToDictionary(kvp => kvp.Code, kvp => kvp.Stock);

            var allProductsQuery = from lineTokens in ReadCsvLines("data\\ProductAll.txt", true, ',')
                                   let product = ParseProduct(lineTokens)
                                   select product;

            using (StreamWriter sw = File.CreateText("data\\updated.txt"))
            {
                foreach (var product in allProductsQuery)
                {
                    if (orderStock.ContainsKey(product.Code))
                    {
                        int newStockValue = orderStock[product.Code];
                        if (newStockValue != product.Stock)
                        {
                            product.Stock = orderStock[product.Code];
                            sw.WriteLine(ToString(product));
                        }
                    }
                }
            }

        }

        static IEnumerable<string[]> ReadCsvLines(string file, bool hasHeader, char delimiter)
        {
            using (CsvReader csr = new CsvReader(new StreamReader(file), hasHeader, delimiter))
            {
                int fieldCount = csr.FieldCount;
                while (csr.ReadNextRecord())
                {
                    List<string> lineTokens = new List<string>();
                    for (int i = 0; i < fieldCount; i++)
                        lineTokens.Add(csr[i]);
                    yield return lineTokens.ToArray();
                }
            }
        }

        static Product ParseProduct(string[] tokens)
        {
            Product product = new Product();

            product.Action = tokens[0];
            product.CategoryPath = tokens[1];
            product.ID = Convert.ToInt32(tokens[2]);
            product.Name = tokens[3];
            product.Code = tokens[4];
            product.Stock = Convert.ToInt32(tokens[5]);

            return product;
        }

        static string ToString(Product product)
        {
            const char SEPERATOR = ',';

            StringBuilder sb = new StringBuilder();
            sb.Append(product.Action.DoubleQuoteString()); sb.Append(SEPERATOR);
            sb.Append(product.CategoryPath.DoubleQuoteString()); sb.Append(SEPERATOR);
            sb.Append(product.ID); sb.Append(SEPERATOR);
            sb.Append(product.Name.DoubleQuoteString()); sb.Append(SEPERATOR);
            sb.Append(product.Code.DoubleQuoteString()); sb.Append(SEPERATOR);
            sb.Append(product.Stock);

            return sb.ToString();
        }
    }
}

您可以根据需要将类放到不同的文件中。例如。将Product类移动到自己的文件。当然还要添加一些错误处理。

希望这有帮助。

相关问题