C#编程-117:文件流FileStream类
2024-06-25 09:51:41 阅读次数:24
编程开发
- 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();
- }
- }
- }
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/mouday/3046597,作者:彭世瑜,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。
上一篇:C#编程-147:线程基础
下一篇:JavaFX使用maven-assembly-plugin打包