首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

为什么关闭文件的语句会出错?该如何处理

2012-02-24 
为什么关闭文件的语句会出错?下面这段代码从input.txt文件读取文本行,过滤掉空行后写入output.txt。编译时

为什么关闭文件的语句会出错?
下面这段代码从input.txt文件读取文本行,过滤掉空行后写入output.txt。
编译时最后两句关闭文件的代码出错,提示是“只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句”。
这是怎么回事啊?

C# code
StreamReader freader = File.OpenText("input.txt");StreamWriter fwriter = File.CreateText("output.txt");string text_line;while ((text_line = freader.ReadLine()) != null){    if (text_line.Length == 0)    {        fwriter.WriteLine(text_line);    }}fwriter.Close;    //这里出错freader.Close;    //这里出错



[解决办法]
Close()
[解决办法]
C# code
            StreamReader freader = File.OpenText("input.txt");            StreamWriter fwriter = File.CreateText("output.txt");            string text_line;            while ((text_line = freader.ReadLine()) != null)            {                if (text_line.Length == 0)                {                    fwriter.WriteLine(text_line);                }            }//C#里面, .后面不带括号的是属性 带括号的是方法..这样说 你能理解吗?           fwriter.Close();                          freader.Close();
[解决办法]
用using语句吧。
[解决办法]
呵呵,我也老是忘记括号,不过一般系统给我指出了我就马上醒悟啦……
方法后面都是要()滴~~不管你有没有参数……

热点排行