坚持使用流作家 [英] stuck up with stream writer

查看:59
本文介绍了坚持使用流作家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string[] file1 =( File.ReadAllLines(textBox1.Text));
        //string[] file2;

        int size = file1.Length;





using (FileStream fs = File.Open( "memory.txt",FileMode.OpenOrCreate))
            {
                using (TextReader r = new StreamReader(fs))
                {
                    for (i = 0; i < size; i++)
                    {
                        array[i] =Convert.ToInt32 (r.ReadLine());
                        listBox1.Items.Add(array[i]);




                    }


                }


            }
            string[] file2;
            using (FileStream fs1 = File.Create("dest.txt"))
            {
                using (TextWriter w = new StreamWriter(fs1))
                {
                    for (i = 0; i < size; i++)
                    {///here i want to add the contents of array1...to dest.txt...
                        
                    }
                }
            }




        }
    }
}



i无法执行.. .stream编写器函数...上面是代码...



textbox1.text将采用txt.file即(memory.txt)...



已添加代码块[/编辑]


i am unable to perform...stream writer function ...above is the code...

textbox1.text will take a txt.file i.e (memory.txt)...

Code block added[/Edit]

推荐答案



你做了很多错事在上面的代码中。



1)从文本框中读取文本而不检查空字符串。

2)将文本框文本读入字符串数组;

3)检查数组的长度为大小:在这种情况下,这将是1,因为只读取了一个文本框。

4)打开一个文件OpenCreate这意味着如果文件不存在,则创建一个;然后用流读取器读取文件,这是不必要的。

5)对于带Size的循环,如果文本框为空,则大小为0,如果不是则则为1 ;但是;

6)从使用OpenCreate打开的文件中读取一行,如果文件刚刚创建,则返回null;并且这将转换为int32,这将抛出尚未捕获的异常。

7)再次使用FileStream,FileCreate和StreamWriter;完全没必要。



您是否考虑以下代码:



Hi,
you are doing a lot of things wrong in your code above.

1) Reading a text from from a text box into file without checking for empty string.
2) Reading the text box text into string array;
3) Checking the length of the array as size: this will be 1 in this case as only one text box has been read.
4) Opening a file with File "OpenCreate" this means if the file doesn''t exists, then create one; then reading the file with stream reader, this is unnecessary.
5) For loop with "Size", if the text box was empty, then the size will be 0, if not then it will be 1; however;
6) Reading a line from the file that was opened with "OpenCreate", if the file was just created, it will return null; and this is converted to int32, this will throw exception that has not been caught.
7) Again using FileStream, FileCreate and StreamWriter; totally unnecessary.

Have you consider the code below:

public void WriteToFile()
        {
            string fileName_1 = "memory.txt";
            string fileName_2 = "dest.txt";
            List<string> readIn = new List<string>();
            FileInfo fi = new FileInfo(fileName_1);

            // Check that the file exists first
            if (!fi.Exists)
            {
                MessageBox.Show("Memory file doesn''t exists!");
                return;
            }
            else
            {
                using (StreamReader sr = new StreamReader(fileName_1, System.Text.Encoding.Default))
                {
                    string str = "";
                    while ((str =sr.ReadLine()) != null)
                    {
                        readIn.Add(str);
                    }
                }
            }

            if (readIn.Count > 0)
            {
                StringBuilder sb = new StringBuilder();

                foreach (string str in readIn)
                {
                    sb.Append(str + "\r"); // add carriage return if necessary.
                }

                using (TextWriter textWriter = new StreamWriter(fileName_2, true)) // append to file if necessary
                {
                    textWriter.Write(sb.ToString());
                }
            }
        }





问候

Jegan



Regards
Jegan


///这里我想将array1的内容添加到dest.txt ...

尝试:

///here i want to add the contents of array1...to dest.txt...
Try:
w.WriteLine(array[i]);


这篇关于坚持使用流作家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆