c#File1的文本在运行时不断替换file2

时间:2017-12-09 01:45:10

标签: c#

我需要的只是file1和file2来显示文件中的文本。 File1工作得很好! File2没那么多。我相信我写file2的方式有问题。因为我创建了一个类,所以我可以将file2的文本转到另一个名为outputfile2的文件,即使这样也行不通。

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

namespace RandomName
{
class Program
{
    static void Main(string[] args)
    {

        string winDir = 
"C:/Users/RandomPerson/Desktop/RandomName/bin/Debug/";

        string fileName = "file1.txt";
        StreamReader reader = new StreamReader(winDir + fileName);

        string outputFileName = "upperfile" + fileName;
        StreamWriter writer = new StreamWriter(outputFileName);

        int n = 0;
        string st = "";
        string upperString = "";
        int n2 = 0;
        string st2 = "";
        string upperString2 = "";



        string fileName2 = "file2.txt";

        StreamReader reader2 = new StreamReader(winDir + fileName2);
        string outputFileName2 = "output" + fileName2;
        StreamWriter writer2 = new StreamWriter(outputFileName2);

        do
        {
            ++n;
            st = reader.ReadLine();  // read one line from disk file
            Console.WriteLine("Line #" + n + ": " + st);  // write to the console
            writer.WriteLine(st);  // write line to disk file instead, using WriteLine() method
            upperString = upperString + "\n" + st;  // append each line to the big string

        }
        while (!reader.EndOfStream);

        do
        {
            ++n2;
            st2 = reader2.ReadLine();  // read one line from disk file
            Console.WriteLine("Line #" + n2 + ": " + st2);  // write to the 
console
            writer2.WriteLine(st2);  // write line to disk file instead, 
using WriteLine() method
            upperString2 = upperString2 + "\n" + st2;  // append each line 
to the big string
        }
        while (!reader2.EndOfStream);
        reader.Close();  
        writer.Close();

        Console.WriteLine("\nHere is the entire file in a string:");
        Console.WriteLine(upperString);
        Console.WriteLine(upperString2);

        UpperString b = new UpperString(upperString);
        UpperString2 c = new UpperString2(upperString2);

        Console.WriteLine("\nThe string in reverse case: ");
        b.showReverseCase();
        Console.WriteLine("\n");

        c.readingFile2();
        c.toNewFile2();

    }

}
}

“B”。是我的另一个班级。我将该类中的代码复制到“c”中。一,改变字符串的名称等。那没用。这就是为什么我认为主要的某些地方出了问题。

这是班级

class UpperString2
{
    private string upperString2;

    public UpperString2() { }

    public UpperString2(string c) { upperString2 = c; }


    public void readingFile2()
    {
        string[] lines = System.IO.File.ReadAllLines("C:/Users/SomeName/Desktop/FolderName/bin/Debug/file2.txt");            

        System.Console.WriteLine("\nAnother Poem  \n");
        foreach (string line in lines)
        {
            // Use a tab to indent each line of the file.
            Console.WriteLine(line);
        }

    }

    public void toNewFile2()
    {
        using (StreamWriter writetext = new StreamWriter("outputfile2.txt"))
        {
            string newText = (upperString2.ToUpper()).ToString();

            writetext.WriteLine(newText);
        }

    }

我对SteamReader和SteamWriter有点新意,这就是为什么我认为我出错了。我不知道是什么。谢谢任何能帮助我在file2中显示文本的人,而不会被file1的文本覆盖!

1 个答案:

答案 0 :(得分:0)

问题是&#34; outputfile2&#34;已由reader2 Main()中的string fileName2 = "file2.txt"; StreamReader reader2 = new StreamReader(winDir + fileName2); string outputFileName2 = "output" + fileName2; //<--outputfile2.txt StreamWriter writer2 = new StreamWriter(outputFileName2) 打开。

toNewFile2()

然后,当您尝试在public void toNewFile2() { using (StreamWriter writetext = new StreamWriter("outputfile2.txt")) { string newText = (upperString2.ToUpper()).ToString(); writetext.WriteLine(newText); } } 中打开相同的文件进行写入时,它会引发异常:

writer2

这是因为对象Main()仍处于活动状态并将文件锁定在using中,并且在不再需要时处置该对象时没有__init__.py语句。

由于您已将代码移至类,因此请调用该类。