StreamReader StreamWriter 冲突?

darthpanda 2017-08-18 11:29:57
新人求助,第一个帖。
在写很简单的一个命令行程序,从一个文件读取内容,处理完之后,写入另一个文件。


StreamReader reader = File.OpenText(@"C:\some\source\file.txt");
while ((line = reader.ReadLine()) != null)
{
....
}
reader.Dispose();
StreamWriter writer = new StreamWriter(?);


到最后这一行试图创建一个 StreamWriter 时候却发现只接受 Stream 参数,不能直接用 String 路径了。

查了下只看到一个说法说可能是另外那个 StreamReader 导致的。可是这里我不是已经 Dispose() 了么……
...全文
334 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dy00544 2017-08-18
  • 打赏
  • 举报
回复
引用 6 楼 darthpanda 的回复:
[quote=引用 3 楼 xdashewan 的回复:] StreamWriter的构造函数重载里包括有string路径,无论你怎么写都是可以传路径的
这里真的不行……4 个重载,都是做参数的 stream[/quote] 我怀疑你是用了假的 StreamWriter了。
dy00544 2017-08-18
  • 打赏
  • 举报
回复
引用 2 楼 darthpanda 的回复:
感谢回复。
不过文档我之前看了,也知道用 using 来操作文件。可我印象中建立 stream再 dispose跟 using 应该是等价的啊。
写这个程序的目的是为了自己学习,我是比较想搞明白我现在这个写法问题出在哪。
请多指教。

一般来说微软推荐使用using而不是手动调用dispose


darthpanda 2017-08-18
  • 打赏
  • 举报
回复
涉及到的完整代码如下。我是想从 raw.txt 里提取出所有 ed2k 链接,然后写到 source.txt 里边去

            LinkedList<string> links = new LinkedList<string>();
            StreamReader reader = File.OpenText(@"C:\Users\zhang\Desktop\raw.txt");
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                int startIdx = line.IndexOf("ed2k://");
                if (startIdx != -1)
                {
                    int endIdx = line.IndexOf("\"", startIdx);

                    if (endIdx != -1)
                    {
                        links.AddLast(line.Substring(startIdx, endIdx - startIdx) + "\n");
                    }
                }
            }
            reader.Dispose();
            StreamWriter writer = new StreamWriter(@"C:\Users\zhang\Desktop\output.txt");
darthpanda 2017-08-18
  • 打赏
  • 举报
回复
引用 3 楼 xdashewan 的回复:
StreamWriter的构造函数重载里包括有string路径,无论你怎么写都是可以传路径的




这里真的不行……4 个重载,都是做参数的 stream
jy251 2017-08-18
  • 打赏
  • 举报
回复
close试试
by_封爱 版主 2017-08-18
  • 打赏
  • 举报
回复
读写txt我都习惯用IO下面那几个静态方法 我觉得非常简便啊.. readalltext 之类的
xdashewan 2017-08-18
  • 打赏
  • 举报
回复
StreamWriter的构造函数重载里包括有string路径,无论你怎么写都是可以传路径的
darthpanda 2017-08-18
  • 打赏
  • 举报
回复
感谢回复。 不过文档我之前看了,也知道用 using 来操作文件。可我印象中建立 stream再 dispose跟 using 应该是等价的啊。 写这个程序的目的是为了自己学习,我是比较想搞明白我现在这个写法问题出在哪。 请多指教。
dy00544 2017-08-18
  • 打赏
  • 举报
回复
引用 楼主 darthpanda 的回复:
新人求助,第一个帖。 在写很简单的一个命令行程序,从一个文件读取内容,处理完之后,写入另一个文件。

StreamReader reader = File.OpenText(@"C:\some\source\file.txt");
while ((line = reader.ReadLine()) != null)
{
              ....
}
reader.Dispose();
StreamWriter writer = new StreamWriter(?);
到最后这一行试图创建一个 StreamWriter 时候却发现只接受 Stream 参数,不能直接用 String 路径了。 查了下只看到一个说法说可能是另外那个 StreamReader 导致的。可是这里我不是已经 Dispose() 了么……
msdn---- https://msdn.microsoft.com/zh-cn/library/system.io.streamwriter.aspx using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace StreamReadWrite { class Program { static void Main(string[] args) { // Get the directories currently on the C drive. DirectoryInfo[] cDirs = new DirectoryInfo(@"c:\").GetDirectories(); // Write each directory name to a file. using (StreamWriter sw = new StreamWriter("CDriveDirs.txt")) { foreach (DirectoryInfo dir in cDirs) { sw.WriteLine(dir.Name); } } // Read and show each line from the file. string line = ""; using (StreamReader sr = new StreamReader("CDriveDirs.txt")) { while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } } } }

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧