asp教程.net c#读写文件应用实例与详解
1、使用filestream读写文件
文件头:
using system;
using system.collections.generic;
using system.text;
using system.io;
读文件核心代码:
byte[] bydata = new byte[100];
char[] chardata = new char[1000];
try
{
filestream sfile = new filestream("文件路径",filemode.open);
sfile.seek(55, seekorigin.begin);
sfile.read(bydata, 0, 100); //第一个参数是被传进来的字节数组,用以接受filestream对象中的数据,第2个参数是字节数组中开始写入数据的位置,它通常是0,表示从数组的开端文件中向数组写数据,最后一个参数规定从文件读多少字符.
}
catch (ioexception e)
{
console.writeline("an io exception has been thrown!");
console.writeline(e.tostring());
console.readline();
return;
}
decoder d = encoding.utf8.getdecoder();
d.getchars(bydata, 0, bydata.length, chardata, 0);
console.writeline(chardata);
console.readline();
写文件核心代码:
filestream fs = new filestream(文件路径,filemode.create);
//获得字节数组
byte [] data =new utf8encoding().getbytes(string);
//开始写入
fs.write(data,0,data.length);
//清空缓冲区、关闭流
fs.flush();
fs.close();
2、使用streamreader和streamwriter
文件头:
using system;
using system.collections.generic;
using system.text;
using system.io;
streamreader读取文件:
streamreader objreader = new streamreader(文件路径);
string sline="";
arraylist linelist = new arraylist();
while (sline != null)
{
sline = objreader.readline();
if (sline != null&&!sline.equals(""))
linelist.add(sline);
}
objreader.close();
return linelist;
streamwriter写文件:
filestream fs = new filestream(文件路径, filemode.create);
streamwriter sw = new streamwriter(fs);
//开始写入
sw.write(string);
//清空缓冲区
sw.flush();
//关闭流
sw.close();
fs.close();
摩尔庄园 安卓官方版v2.9.24091002S
摩尔庄园很多小伙伴一定都在小时候玩过,但随着我们年龄的增长淡
梦幻庄园最新版 官方正版v1.0.0
梦幻庄园是一款非常好玩的模拟经营类游戏,玩家在游戏中有一个属
合并梦幻岛免内购版 v2.1.0
合并梦幻岛无限内购版是游戏的破解版本,在该版本中为玩家解锁了
无悔华夏魅族版 2025最新版v3.5.194
无悔华夏魅族版是一款超级好用的模拟经营游戏。在这里玩家们需要
月影别墅官方安装包2025版 安卓版v2.3.24
月影别墅官方安装包2025版是一款匠心独具的乙女向剧情游戏,