C#编程-117:文件流FileStream类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
 
namespace FileStreamReadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\Users\pengshiyu\Desktop\source\test.txt";
            byte[] bufferByte=new byte[100];
            char[] bufferChar=new char[100];
 
            try
            {
                FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.ReadWrite);
                fs.Seek(0,SeekOrigin.Begin);
                fs.Read(bufferByte,0,100);
                Decoder dc = Encoding.Default.GetDecoder();
                dc.GetChars(bufferByte,0,bufferChar.Length,bufferChar,0);
                Console.WriteLine(bufferChar);
                Console.WriteLine("读取完成!");
 
                bufferChar = "这短话是新的,试试能不能写进去".ToCharArray();
                Encoder en = Encoding.Default.GetEncoder();
                en.GetBytes(bufferChar,0,bufferChar.Length,bufferByte,0,true);
                fs.Seek(0,SeekOrigin.Begin);
                fs.Write(bufferByte,0,bufferByte.Length);
                fs.Close();
                Console.WriteLine("写入完成!");
            }
 
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
    }
}

C#编程-117:文件流FileStream类C#编程-117:文件流FileStream类C#编程-117:文件流FileStream类C#编程-117:文件流FileStream类C#编程-117:文件流FileStream类C#编程-117:文件流FileStream类C#编程-117:文件流FileStream类C#编程-117:文件流FileStream类C#编程-117:文件流FileStream类C#编程-117:文件流FileStream类C#编程-117:文件流FileStream类

上一篇:C#编程-118:写入文件StreamWriter类


下一篇:《Unity着色器和屏幕特效开发秘笈》—— 3.6 金属与软高光