复制到另一个文本文件时如何覆盖或跳过特定行

时间:2019-01-19 20:18:48

标签: c# streamreader streamwriter

我有一个程序,用户可以在其中设置银行帐户。它将名称,地址,余额和帐户类型(按顺序)写入基本文本文件。我正在尝试为用户提供一种功能,使其能够通过提供新的名称和地址来更新其帐户。余额和帐户类型无法编辑,而是从基本文件中提取的。

该程序当前将新数据写入一个临时文件,该文件最终将覆盖其内容。但是,它也会将用户的旧数据写入文件,这不是我想要的。当前,文件将同时包含旧数据(在用户进行更改之前)和新数据(用户输入要更新的内容)。 我想要一些有关如何覆盖或跳过临时文件中的旧数据,而仅保留新数据和其他帐户的帮助。

我尝试使用String.Replace方法来实现此功能,并使用for循环为每个数据项对其进行迭代(如果用户选择查看数据的选项,则使用for循环显示用户的数据, ),但它没有按预期的功能运行(它没有替代文字或执行任何操作)。

 else if (ChoiceInput == "3")
                //Choice 3 - Update account details.
                {
                    //Check that the account exists.

                    using (FileStream fs2 = new FileStream(FileName, FileMode.Open, FileAccess.ReadWrite))
                    {
                        StreamReader sr2 = new StreamReader(fs2);
                        StreamWriter sw2 = new StreamWriter(fs2);
                            Console.Write("Enter the account name: ");
                            string NameCheck = Console.ReadLine();
                            string NameValidation;
                        try
                        {
                            NameValidation = sr2.ReadLine();
                            string NameReplace = NameValidation;
                            NameReplace.Replace(NameValidation, "");
                            sw2.Write(NameReplace);
                            while (NameValidation != null)
                                {
                                    if (NameValidation.Contains(NameCheck))
                                    //Account exists.
                                    {
                                        for (int i = 0; i < 3; i++)
                                        {
                                        if (i == 1)
                                        {
                                            string ReadReplace = sr2.ReadLine();
                                            ReadReplace.Replace(ReadReplace, "" + "\r\n");
                                            sw2.Write(NameReplace);
                                            Console.WriteLine(ReadReplace);
                                        }
                                        else if (i == 2)
                                        {
                                            string ReadReplace = sr2.ReadLine();
                                            ReadReplace.Replace(ReadReplace, "" + "\r\n");
                                            sw2.Write(NameReplace);
                                            Console.WriteLine(ReadReplace);
                                        }
                                        }
                                    }
                                using (StreamWriter sw = new StreamWriter(FileNameTemp, true))
                                    {
                                    //Enter new details. It writes these details to the temporary file.
                                    Console.WriteLine("Account found.");
                                        Console.WriteLine("Enter new name: ");
                                        string AccountName = Console.ReadLine();
                                        sw.WriteLine(AccountName);
                                        Console.WriteLine("Enter new address: ");
                                        string AccountAddress = Console.ReadLine();
                                        sw.WriteLine(AccountAddress);
                                        NameValidation = null;
                                        for (int i = 0; i < 3; i++)
                                        //Loop that iterates twice, writing the balance and account type to the temporary file. This data cannot be changed.
                                        {
                                            string Write = sr2.ReadLine();
                                            if (i == 1)
                                            //Balance.
                                            {
                                                sw.WriteLine(Write);
                                            }
                                            else if (i == 2)
                                            //Account type.
                                            {
                                                sw.WriteLine(Write);
                                            }
                                        }
                                    }
                                    using (StreamReader sre = new StreamReader(FileName, true))
                                    {
                                        using (StreamWriter sw = new StreamWriter(FileNameTemp, true))
                                        {
                                            string Lines;
                                            int Counter = 0;
                                            while ((Lines = sre.ReadLine()) != null)
                                            //While the program still has lines to go through, it will continue writing to the text file.
                                            {
                                                Console.WriteLine(Lines);
                                                sw.Write(Lines + "\r\n");
                                                Counter++;
                                            }
                                        }
                                        using (FileStream fs = new FileStream(FileNameTemp, FileMode.Truncate))
                                        //Clears the temporary file, as not doing so would cause issues.
                                        {
                                        }
                                        Console.WriteLine("Account details updated!");
                                    }
                                }
                        }
                        finally
                        {
                            Console.ReadLine();
                        }
                        }
                    }

更新:我添加了一个示例以说明我要执行的操作。 NameValidation下的第一个for循环应该通过将空文件替换为旧文件来删除基础文件上的旧文件。但是,它不能按预期工作。它不向基本文件写入任何内容,然后崩溃,并显示错误“ System.IO.IOException:'该进程无法访问文件'C:\ program1 \ Bank.txt',因为该文件正在被另一个进程使用。 '“,当它带有StreamReader sre时。 但是,它会将下一个帐户的余额和帐户类型写入临时文件。

1 个答案:

答案 0 :(得分:0)

好的。有关如何进行的一些提示。

这就是您的文本文件的格式

float

在插入之前,请检查名称是否已存在。如果是,则获取ID(如果存在testname,则必须获取ID值,即1)。

如果获得此ID,则可以解析文件并使用StreamWriter

更新它们

[或]

阅读姓名和地址,并替换为string.replace

相关问题