ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – 为什么FileStream不作为Streamwriter的参数写入文本文件?

2019-07-17 18:05:43  阅读:308  来源: 互联网

标签:c filestream streamwriter


在下面的代码中,当使用以下语句时,我能够将字符串’fullname’的内容写入指定目录中的文本文件:
System.IO.File.WriteAllText(path,fullname);
但是,如果我将字符串路径写入FileStream对象(指定了参数),然后将该FileStream对象作为参数传递给StreamWriter对象,则会创建该文件,但不会写入任何内容.

第一次尝试:注释掉System.IO.File.WriteAllText(path,fullname);并使用它上面的三条线.这将创建文件,但不会将任何内容写入文件.

第二次尝试:取消注释System.IO.File.WriteAllText(path,fullname);陈述并评论它上面的三行.这可以根据需要执行.

这是完整的代码块:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileInputOutput
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use the Split() method of the String Class
            string fullname = " Robert Gordon Orr ";
            fullname = fullname.Trim();
            string[] splitNameArray = fullname.Split(' ');
            Console.WriteLine("First Name is: {0}", splitNameArray[0]);
            Console.WriteLine("Middle Name is: {0}", splitNameArray[1]);
            Console.WriteLine("Last Name is: {0}", splitNameArray[2]);
            Console.WriteLine("Full name is: {0}", fullname);
            string path = @"C:\Programming\C#\C# Practice Folder\Console Applications\FileInputOutput\textfile.txt";

            FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
            StreamWriter toFile = new StreamWriter(fs);
            toFile.Write(fullname);

            //System.IO.File.WriteAllText(path, fullname);`enter code here`

            Console.ReadLine();

        }
    }
}

解决方法:

正如其他人所说:必须在.NET中刷新流才能将它们写入磁盘.这可以手动完成,但我只需更改您的代码以在您的流上使用语句:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileInputOutput
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use the Split() method of the String Class
            string fullname = " Robert Gordon Orr ";
            fullname = fullname.Trim();
            string[] splitNameArray = fullname.Split(' ');
            Console.WriteLine("First Name is: {0}", splitNameArray[0]);
            Console.WriteLine("Middle Name is: {0}", splitNameArray[1]);
            Console.WriteLine("Last Name is: {0}", splitNameArray[2]);
            Console.WriteLine("Full name is: {0}", fullname);
            string path = @"C:\textfile.txt";

            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                using (StreamWriter toFile = new StreamWriter(fs))
                {
                    toFile.Write(fullname);
                }
            }
            //System.IO.File.WriteAllText(path, fullname);`enter code here`

            Console.ReadLine();

        }
    }
}

在流上调用Dispose()(如隐式使用)会导致在使用块结束时刷新和关闭流.

标签:c,filestream,streamwriter
来源: https://codeday.me/bug/20190717/1490825.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有