当前位置: 移动技术网 > IT编程>开发语言>c# > c#文件的I/O基本操作

c#文件的I/O基本操作

2019年07月18日  | 移动技术网IT编程  | 我要评论
文件是一些永久存储及具有特定顺序的字节组成的一个有序的,具有名称的集合。与文件有关的概念是目录路径和磁盘存储等。流提供了一种向后备存储写入字节和从后备存储读取字节的方式。后

文件是一些永久存储及具有特定顺序的字节组成的一个有序的,具有名称的集合。与文件有关的概念是目录路径和磁盘存储等。流提供了一种向后备存储写入字节和从后备存储读取字节的方式。后备存储包裹用文件存储或用内存(数组)存储,以及网络,cd等。

基本文件的i/o

命名空间为system.i/o,程序集为mscorlib(在mscorlib.dll中)
抽象基类stream支持读取和写入字节。stream集成了异步支持,其默认实现根据其相应的异步方法来定义同步读取和写入。
所有表示流的类都是从stream类继承的。stream类及其派生类提供数据源和存储库的一般视图,使程序员不必了解操作系统和基础设备的具体细节。
流涉及3个基本操作:从流读取,向流写入以及查找和修改流内当前位置。根据基础数据源或存储库,流可能只支持这些功能中的一部分。例如,networkstream不支持查找。stream的canread,canwrite和canseek属性及其派生类决定不同的流所支持的操作。

stream类

stream是所有流的抽象基类。流是字节序列的抽象概念,如文件,输入输出设备,内部进程通信管道或tcp/ip套接字。stream类及其派生类提供这些不同类型的输入输出的一般视图,使程序员不必了解操作系统和基础设备的具体细节。
如果用memorystream初始化流,流的后备存储是内存,容量随数据动态的增加。如果用filestream初始化流,流的后备存储是文件,对流的操作视同对文件的操作。

下面的例子使用stream..writebyte和stream..read写入和读取数据块

复制代码 代码如下:

using system;
using system.io;
public class block
{
public static void main()
{
stream s=new memorystream();//产生一个流,它的后备存储是内存
//将一个字节写入流内的当前位置,位置推进一个字节
for(int i=0;i<100;i++)
{
s.wtritebyte((byte)i);
}
s.positon=0;//流中位置设置为0
byte[]betes=new byte[1000];
//请求从流中读取的最大字节数等于流的长度
int numbytestoread=(int)s.length;
int numbytesread=0;//偏移量设置为0
while(numbytestoread>0)
{
//s.read从当前流读取字节序列,并将此流中的位置提升读取的字节数
//返回值n是实际读取的字节数,如果已到达流的末尾则为零(0)
int n=s.read(   bytes, //数组bytes接收从流中读取的字节
numbytesread,   //数组bytes的偏移量,从偏移量开始存储数据
numbytestoread);//请求读取的最大字节数
if(n==0)
{
break;
}
numbytesread+=n;
numbytestoread-=n;
}
s.close();
//现在请求读取的字节数numbytestoread为0,偏移量numbytesread应该为100
console.writeline("number of bytes read:"+numbytesread);
}
}

输出如下:

复制代码 代码如下:

number of bytes read:100


file类
file类提供用于创建,复制,删除,移动和打开文件的静态方法,并协助创建filestream对象。也可将file类用于获取和设置文件属性或有关文件创建,访问及写入操作的datetime信息。由于所有的file方法都是静态的,因此如果只想执行一个操作,则使用file方法的效果比使用相应的fileinfo实例方法可能更高

下面演示file类的一些主要成员。代码中使用using语句定义一个范围。将下面的代码放入main方法。

复制代码 代码如下:

string path=@"..\...\mytest.txt";//mytest.txt位于项目的文件夹
if(!file.exits(path))
{
//using语句用于定义一个范围,再次范围的末尾将释放对象sw
//streamwriter实现一个textwriter,使其以特定的编码utf-8向流中写入字符
//file.createtext创建或打开一个文件用于写入utf-8编码的文本
using(streamwriter sw=file.createtext(path))
{
sw.writeline("hello");
sw.writeline("and");
sw.writeline("welcome");
}
}
//使用streamreader读取标准文本文件的各行信息
using (streamreader sr=file.opentext(path))
{
string s="";
//从当前的流中读出一行字符将数据作为字符串返回
while((s=sr.readline())!=null)
{
//显示mytext.txt的内容“hello/and/welcome”到屏幕
console.writeline(s);
}
}
try
{
string path2=path+"temp";
file.delete(path2);//确保目标文件不存在
file.copy(path,path2);//复制文件
console.writeline("{0}was copied to {1}",path,path2);
file.delete(path2);
console.writeline("{0}was successfully deleted.",path2);
}
catch(exception e)
{
console.writeline("the process failed:{0}",e.tostring());
}

运行结果为程序在本项目文件夹建立一个文本文件mytext.txt,屏幕显示为:
复制代码 代码如下:

hello/and/welcome
..\..\mytest.txt was copied to..\..\mytest.txttemp
..\..\mytest.txt was successfully deleted

 

fileinfo类

fileinfo类提供创建,复制,删除,移动和打开文件的实例方法,并且帮助创建filestream对象,如果打算多次重用某个对象,可考虑使用fileinfo的实例方法,而不是file类的相对静态方法,以内并不总是需要安全检查
下面的例子是使用fileinfo构造函数创建两个文件,并接着对其进行写入,读取,复制和删除操作

复制代码 代码如下:

string path=@"..\..\mytext.txt"
fileinfo fil=new fileinfo(path);//fileinfo提供的方法是非静态方法,必须实例化
if(!fil.exists)
{
using(streamwriter sw=fil.creatertext())
{
sw.writeline("hello");
sw.writeline("and");
sw.writeline("welcome");
}
}
using(streamreader sr=fil.opentext())
{
string s="";
while((s=sr.readline())!=null)
console.writeline(s);
}
try
{
string path2=path+"temp";
fileinfo fi2=new fileinfo(path2);
fi2.delete();//确保目标文件不存在
fi1.copyto(path2);//复制文件
console.writeline("{0}was copied to {1}",path,path2);
fi2.delete();
console.writeline("{0}was successfully deleted",path2);
}
catch(exception e)
{
console.writeline("the process failed:{0}",e.tostring());
}

运行结果为

复制代码 代码如下:

hello/and/welcome
..\..\mytest.txt was copied to..\..\mytest.txttemp
..\..\mytest.txt was successfully deleted


下面的一个例子是显示fileinfo的一些主要成员
复制代码 代码如下:

//创建磁盘上给你唯一命名的零字节的临时文件并返回该文件的完整路径
//此方法创建带.tmp文件拓展名的临时文件
string path=path.gettempfilename();
//初始化fileinfo类的新实例,它作为文件路径的包装
fileinfo fil=new fileinfo(path);
if(!fil.exists)
{
//fil.createtext创建吸入新文本文件的streamwriter
//默认情况下,将向所有用户授予对于新文件的完全读写访问权限
using(streamwriter sw=fil.createtext())
{
sw.writeline("hello");
sw.writeline("and");
sw.writeline("welcome");
}
}
using(streamreader sr=fil.opentext())//打开文件读取
{
string s="";
while((s=sr.readline())!=null)
console.writeline(s);
}
try
{
string path2=path.gettempfilename();
fileinfo fi2=new fileinfo(path2);
fi2.delete();
fi1.copyto(path2);
console.writeline("{0}was copied to{1}",path,path2);
fi2.delete();
console.writeline("{0}was successfully deleted.",path2)
}
catch(exception e)
{
console.writeline("the process failed:{0}",e.tostring());
}

运行结果如下:
复制代码 代码如下:

c:\documents and settings\administrator\local settings\temp\temp10.tmp was copied to
c:\documents and settings\administrator\local settings\temp\temp11.tmp
c:\documents and settings\administrator\local settings\temp\temp11.tmp was successfully deleted

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网