//
//将文件方法包装在一个带有易用方法的简单类中
//
public class csFile
{
private string fileName;
StreamReader ts;
StreamWriter ws;
private bool opened, writeOpened;
private void init()
{
opened = false;
writeOpened = false;
}
public csFile()
{
init();
}
public csFile(string file_name)
{
fileName = file_name;
init();
}
//读文件
public bool OpenForRead(string file_name)
{
fileName = file_name;
try
{
ts = new StreamReader(fileName);
opened = true;
}
catch(FileNotFoundException e)
{
return false;
}
return true;
}
public bool OpenForRead()
{
return OpenForRead(fileName);
}
public string readLine()
{
return ts.ReadLine();
}
//写文件
public void writeLine(string s)
{
ws.WriteLine(s);
}
public bool OpenForWrite(string file_name)
{
try
{
ws = new StreamWriter(file_name);
fileName = file_name;
writeOpened = true;
return true;
}
catch(FileNotFoundException e)
{
return false;
}
}
public bool OpenForWrite()
{
return OpenForRead(fileName);
}
}
csFile.cs
最新推荐文章于 2025-01-22 10:17:39 发布
本文介绍了一个用于简化文件读写操作的C#类。该类提供了易于使用的接口来打开文件进行读取或写入,并包含基本的方法如读取一行或写入一行。通过这个简单的封装,开发者可以更方便地处理文件I/O操作。
3万+

被折叠的 条评论
为什么被折叠?



