浅学C#(21)——流(2)读取写入文件、串行化对象、监控文件

本文介绍了C#中如何使用FileStream、StreamWriter和StreamReader进行文件读写,探讨了BinaryReader和BinaryWriter在二进制数据读写的应用,以及如何利用串行化对象实现数据持久化。同时,还讲解了如何通过FileSystemWatcher监控文件系统变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FileStream对象

表示在磁盘或网络路径上指向文件的流,提供了在文件中读写字节的方法
构造方法
public FileStream ( string path, FileMode mode )
public FileStream ( string path, FileMode mode, FileAccess access )
Read 用于只读
Write 用于只写
ReadWrite 用于读写

FileStream  fs=new FileStream(“D:\\1.txt”,FileMode.Open,FileAccess.Read,FileShare.None);
//等价于
FileStream  fs=File.Open(“D:\\1.txt”,FileMode.Open,FileAccess.Read,FileShare.None);

FileStream  fs=new FileStream(“D:\\1.txt”,FileMode.OpenOrCreate,FileAccess.Write,FileShare.None);
//等价于
FileStream  fs=File.OpenWrite(“D:\\1.txt”);

文件位置
FileStream类维护内部文件指针,该指针指向文件中进行下一次读写操作的位置

public override long   Seek ( long offset, SeekOrigin origin ) 

offset为文件指针以字节为单位的移动距离
origin为开始计算的起始位置

  • SeekOrigin.Begin Current End
aFile.Seek(8, SeekOrigin.Begin);
aFile.Seek(2, SeekOrigin.Current);
aFile.Seek(-5, SeekOrigin.End);

读取数据
不仅可以读取文本文件,还可以读取图像和声音文件
public override int Read ( byte[] array, int offset, int count )
array为字节数组
offset为字节数组中开始写入数据的位置
count指定从文件中读出多少字节
int ReadByte( )
从文件中读取一个字节,并将读取位置提升一个字节

写入数据
public override void Write ( byte[] array, int offset, int count )
使用从缓冲区读取的数据将字节块写入该流
void WriteByte( byte value )
将一个字节写入文件流的当前位置

创建文件

用File创建文件

using    System.IO;
string   strTempPath=Path.GetTempPath( );
string   strFileName=Path.Combine(strTempPath, “test.txt”);
FileStream   aFile=File.Create(strFileName);
if   ( File.Exists(strFileName) )
   Console.WriteLine(“File  {0}  have created”, strFileName);
else
   Console.WriteLine(“File {0} created failed”, strFileName);
aFile.Close( );
File.Delete(strFileName);

用FileInfo创建文件

FileInfo   bFile=new  FileInfo(strFileName);
FileStream   cFile=bFile.Create();
if   ( bFile.Exists)
   Console.WriteLine(“File  {0}  have created”,strFileName);
else
   Console.WriteLine(“File {0} created failed”, strFileName);
cFile.Close( );
bFile.Delete( );

从随机访问文件中读取数据

 byte[] byData = new byte[200];
 char[] charData = new char[200];
 try  {
    FileStream aFile = new FileStream("E:/程序/f2.java", FileMode.Open);
    aFile.Seek(90, SeekOrigin.Begin);
    aFile.Read(byData, 0, 200);
  }
 catch (IOException e)
   {
 	Console.WriteLine("An IO exception has been thrown");
    Console.WriteLine(e.ToString());
    Console.ReadKey();
    return;
   }
	Decoder d = Encoding.UTF8.GetDecoder();
    d.GetChars(byData, 0, byData.Length, charData, 0);
	Console.WriteLine(charData);
	Console.ReadKey();
}

将数据写入随机访问文件

byte[] byData;
char[] charData;
 try
 {
    FileStream aFile = new FileStream("E:/Temp.txt",   FileMode.Create);
    charData = "My pink half of the drainpipe.".ToCharArray();
    byData = new byte[charData.Length];
    Encoder e = Encoding.UTF8.GetEncoder();
    e.GetBytes(charData, 0, charData.Length, byData, 0, true);
    aFile.Seek(0, SeekOrigin.Begin);
    aFile.Write(byData, 0, byData.Length);
 }
catch (IOException ex)
 {
    Console.WriteLine("An IO exception has been thrown!");
    Console.WriteLine(ex.ToString());
    Console.ReadKey();
    return;
 }
StreamWriter对象
  • StreamWriter的构造方法
  • public StreamWriter ( string path )
  • public StreamWriter ( string path, bool append )
    append 为false,则创建一个新文件,或截取现有文件并打开它
    append 为true,则打开文件,保留原有数据。如果找不到文件,则创建一个新文件
  • public StreamWriter ( Stream stream )
FileSt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值