流作家/读者 [英] Stream Writer/Reader

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

问题描述

你好,
我如何在Windows窗体中使用StreamWriter而不重置我的函数xxx.txt文件?我在一个小型的Model View Controller项目上工作.
例如:在我的控制器类中:

Hello,
how do i use StreamWriter in a windows form without my function resetting the xxx.txt file ?I''m workng on a small Model View Controller project.
ex: in my controller class:

  FileInfo f=new FileInfo("LibraryFiles.txt");
  StreamWriter Swriter;

  public void AddFile(Object ob)
  {
   Swriter=f.CreateText();
   Swrite.Writeline(ob);
   Swrite.Write(Swrite.NewLine);
   Swriter.Close();
   //if i close my file here then i can''t put anymore Object
   //get error can''t put in closed file
  }

 //i only can get the last element enterred because the file is
 //recreated all the time i call this method.
 //In My GUI it''s fine other methods work perfectly.

buttonclick ...()
{
   myInstance.AddFile(object)
}

推荐答案

您会遇到什么错误?它会引发异常还是无声地失败?如果您编辑问题以包含此信息,可能会有所帮助.

Close()之后,您需要在SWriter上调用Dispose(),或者最好在using语句中使用它,以使框架为您执行此操作.

如果要附加数据而不是覆盖数据,则需要使用带有适当参数的Open而不是CreateText().
What error do you get? Does it throw an exception or fail silently? It might be helpful if you edit your question to include this info.

You need to call Dispose() on SWriter after you Close() it, or better yet use it in a using statement to have the framework do it for you.

If you want to append data instead of overwriting it then you''ll need to use Open with the proper parameters instead of CreateText().


这一切对我来说都是有缺陷的.为什么将对象传递给此方法?您为什么要关闭文件,而再也不会再次打开它?为什么不使用try/catch/finally块来确保始终正确关闭并重新打开文件?在创建文本文件时,为什么不只是将字符串作为参数(无论如何,这都是最终要写的内容),如果要追加到文件,请使用File.AppendAllText.或File.WriteAllText如果您要删除旧文件并创建一个新文件?
This all seems flawed to me. Why would you pass an object to this method ? Why would you close the file, and never open it again ? Why wouldn''t you use a try/catch/finally block to make sure the file is always properly closed and reopened ? As you''re creating a text file, why not just take a string as a parameter ( that''s what you end up writing in any case ), and use File.AppendAllText, if you want to append to your file. or File.WriteAllText if you want to erase the old file and create a new one ?


您好,
-我得到的错误是无法在关闭的文件中写入"的异常
-该函数具有一个对象参数,因为WriteLine()具有一个重载,可以接收对象参数,字符串,整数...只要对象覆盖ToString(),它就可以正常工作.

但这不是我的问题,我想知道如何在程序中放置代码.相同的代码可在控制台应用程序中使用!

这是我第一次使用这个论坛,我不知道是否可以发送代码.如果不是-对不起:)
这是控制台中的代码:

Hello,
-The error I get is an exception "cannot write in a closed file"
-The function has an object argument because WriteLine() has an overload that receives an object argument, string, int...as long as the object overrides ToString() it works okay.

But that''s not my problem I''d like to know how to position the codes in my program. The same code works in Console application !

It''s my first time to use this forum I don''t know if sending codes is okay. In case it''s not - Sorry :)
Here is the code in console:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace StreamReadWrite
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p.Name = "jeannette";
            p.Id = 666;
            Person p1 = new Person();
            p1.Name = "Jonathan";
            p1.Id = 43443;
            //Create and Write into a file
            FileInfo f = new FileInfo("ReadWriteTest.txt");
            StreamWriter Swriter = f.CreateText();
            Swriter.WriteLine(" mama is my witness");
            Swriter.WriteLine(p);
            Swriter.WriteLine(p1);
            Swriter.WriteLine(" I love programming in c#");
            Swriter.Write(Swriter.NewLine);
            Swriter.Close();
            Console.WriteLine("The file ReadWriteTest has been created ");
            //
            //Read from the file
            StreamReader Sreader = File.OpenText("ReadWriteTest.txt");
            string input = "";
            while ((input = Sreader.ReadLine()) != null)
            {
                Console.WriteLine(input.ToString());
            }
            Sreader.Close();
            //return 0;
            Console.Read();
        }
        public class Person
        {
            public string Name { get; set; }
            public int Id { get; set; }
            public override string ToString()
            {
                return " Name : "+Name+" "+"Id :"+Id;
            }
        }
    }
}


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

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