从硬盘插入文件

时间:2015-05-18 01:52:13

标签: c# visual-studio-2013 streamwriter

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

中有2个文件

我正在尝试在我的表单中让用户可以在文件中插入新单词,但我不知道StreamWritersStreamReaders期间是否有效。

我认为我的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);

我真的不知道作家的问题是什么。

2 个答案:

答案 0 :(得分:0)

您需要关闭StreamReaderStreamWriter,在onder中保存您的书面文字,您需要关闭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();
        }
    }

我试过这个,效果很好

答案 1 :(得分:0)

问题在于,每当我完成写作时我都必须刷新,所以在寻求开始并写入后我应该刷新,在方法结束时我应该关闭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();
        }