当前位置:编程学习 > C#/ASP.NET >>

c#文件流读写文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace 文件流
{
    class Program
    {
        static void Main(string[] args)
        {
            //C#文件流写文件,默认追加FileMode.Append
            string msg = "ok";
            byte[] myByte = System.Text.Encoding.UTF8.GetBytes(msg);
            using (FileStream fsWrite = new FileStream(@"C:\1.txt", FileMode.Append))
            {
                fsWrite.Write(myByte, 0, myByte.Length);
            };
 
            //c#文件流读文件
            using (FileStream fsRead = new FileStream(@"C:\1.txt", FileMode.Open))
            {
                int fsLen = (int)fsRead.Length;
                byte[] heByte = new byte[fsLen];
                int r = fsRead.Read(heByte, 0, heByte.Length);
                string myStr = System.Text.Encoding.UTF8.GetString(heByte);
                Console.WriteLine(myStr);
                Console.ReadKey();
            }
        }
    }
}

 

 

摘自 黑牛打龙

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,