从硬盘插入文件

Insert to file from Hard Disk

我正在尝试制作一个小应用程序(字典),将英语翻译成阿拉伯语,反之亦然。 我在调试文件夹 English.txt 和 Arabic.txt.

中有 2 个文件

我正在尝试在我的表单中让用户可以向文件中插入新词,但我不知道 StreamWriters 是否在工作,而 StreamReaders 是。

我认为我的 Seek 语句也不正确。

这是我试过的代码:

    FileStream englishFile = new FileStream("English.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
    StreamReader engSR = new StreamReader(englishFile);
    StreamWriter engSW = new StreamWriter(englishFile);
    FileStream arabicFile = new FileStream("Arabic.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
    StreamWriter arabSW = new StreamWriter(arabicFile);
    StreamReader arabSR = new StreamReader(arabicFile);


    string line = engSR.ReadLine();
    string[] arr = line.Split('|');
    int numberOfLines = int.Parse(arr[1]) + 1;


    englishFile.Seek(0, SeekOrigin.Begin);
    engSW.WriteLine("*|{0}", numberOfLines);
    arabSW.WriteLine("*|{0}", numberOfLines);


    englishFile.Seek(0, SeekOrigin.End);
    line = engSR.ReadLine();
    engSW.WriteLine("{0}|{1}", numberOfLines, englishin.Text);

    arabicFile.Seek(0, SeekOrigin.End);
    line = arabSR.ReadLine();
    arabSW.WriteLine("{0}|{1}", numberOfLines, arabicin.Text);


    arabicFile.Close();
    englishFile.Close();

其中 englishinarabicin 是两个文本框。 遗憾的是,文件中没有发生任何变化。

我也试过了:

string line = engSR.ReadLine();
        string[] arr = line.Split('|');
        int numberOfLines = int.Parse(arr[1]) + 1;


        englishFile.Seek(0, SeekOrigin.Begin);
        engSW.WriteLine("*|{0}", numberOfLines);
        arabSW.WriteLine("*|{0}", numberOfLines);


        //englishFile.Seek(0, SeekOrigin.End);

        line = engSR.ReadLine();
        while (line != null)
            line = engSR.ReadLine();
        engSW.WriteLine("{0}|{1}", numberOfLines, englishin.Text);

        //arabicFile.Seek(0, SeekOrigin.End);

        line = arabSR.ReadLine();
        while (line != null)
            line = arabSR.ReadLine();
        arabSW.WriteLine("{0}|{1}", numberOfLines, arabicin.Text);

真不知道作者有什么问题

您需要关闭您的StreamReaderStreamWriter,为了保存您的文字,您需要关闭StreamWriter

额外

https://msdn.microsoft.com/en-us/library/system.io.streamwriter%28v=vs.110%29.aspx

System.IO是IDisposable,读写后要释放。

 private static void Main()
    {
        FileStream englishFile = new FileStream("English.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
        StreamReader engSR = new StreamReader(englishFile);
        StreamWriter engSW = new StreamWriter(englishFile);
        FileStream arabicFile = new FileStream("Arabic.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
        StreamWriter arabSW = new StreamWriter(arabicFile);
        StreamReader arabSR = new StreamReader(arabicFile);

        char delimiter = '|';
        try
        {
            englishFile.Seek(0, SeekOrigin.Begin);

            while(!engSR.EndOfStream){
                string line = engSR.ReadLine();
                string[] arr = line.Split(delimiter);

                for (int j = 0; j < arr.Count();j++ )
                {
                    if (j > 0)
                    {
                         arabSW.Write(delimiter);
                    }
                    String outStr = ConvertStr(arr[j]);
                    arabSW.Write(outStr);
                }

                arabSW.Write(arabSW.NewLine);
            }
        }
        catch
        {

        }
        finally
        {
            arabSW.Close();
            //arabSR.Close();
            engSR.Close();
            //engSW.Close();
            arabicFile.Close();
            englishFile.Close();
        }
    }

我试过了,效果很好

问题是我必须在完成写入后进行刷新,所以在寻找开始并写入之后我应该刷新并且在方法结束时我应该像他们说的那样关闭 StreamWriter。

            string line = engSR.ReadLine();
            string[] arr = line.Split('|');
            int numberOfLines = int.Parse(arr[1]) + 1;


            englishFile.Seek(0, SeekOrigin.Begin);
            engSW.WriteLine("*|{0}", numberOfLines);
            arabicFile.Seek(0, SeekOrigin.Begin);
            arabSW.WriteLine("*|{0}", numberOfLines);
            engSW.Flush();
            arabSW.Flush();


            englishFile.Seek(0, SeekOrigin.End);
            line = engSR.ReadLine();
            engSW.WriteLine("{0}|{1}", numberOfLines, englishin.Text);

            arabicFile.Seek(0, SeekOrigin.End);
            line = arabSR.ReadLine();
            arabSW.WriteLine("{0}|{1}", numberOfLines, arabicin.Text);
        }
        catch
        {

        }
        finally
        {
            engSW.Close();
            arabSW.Close();
            arabSR.Close();
            engSR.Close();
            arabicFile.Close();
            englishFile.Close();
        }