c#,文件读写的操作
1、文件的读与写:
在c#中,读写文件的操作流程:
1)创建一个FileStream
2)创建阅读器或者写入器(针对不同情况的不同需求)
3)执行读或者写的操作
4)关闭阅读器或者写入器
5)关闭这个FileStream
其中,使用FileStream的时候,需要引用命名空间:
using System.IO;
2、文件流(FileStream):
step ①:创建文件流:
在读写文件的过程中,首先要开启文件流;
在c#中,使用FileStream 类创建文件流。
FileStream(String FilePath , FileMode)
在这里,传入了两个参数,FilePath 是对应文件读写的path,
FileMode 是对要进行读写文件的打开方式。
其中,参数FileMode是枚举(enum)类型,FileMode有如下成员:
1)Create:新建一个指定名称的文件(如果该文件存在,则替换旧文件)
2)CreateNew:新建一个文件。
3)Open:打开一个文件,使用它的时候,打开的指定文件必须存在,否则会发生异常。
4)OpenOrCreate:类似于成员Open,如果文件不存在,则新创建一个并打开。
step ②:关闭文件流:
使用完毕读写器,记得要关闭读写器,这里用到了FileStream 对象中的.close()方法。
3、文件读写器:
1)StreamWriter写入器:
首先要创建好文件流,然后再创建阅读器或者写入器(根据需求);
在这里,写入器就用到了StreamWriter类,用来将数据写入文件流中,
打开文件流后就可以new一个StreamWriter的对象。
StreamWriter可以调用的方法如下:
StreamWriter.Write();//写入流。 StreamWriter.WriteLine();//写入一行数据后自动换行。 StreamWriter.Close();//关闭写入器
2)StreamReader读取器:
同理,使用前要创建好文件流,然后创建读取器(StreamReader);
读取器用到了StreamReader类,调用方法如下:
StreamReader.ReadLine();//读取文件流中的一行数据,返回字符串。 StreamReader.ReadToEnd();//从当前位置读取到结束,返回字符串。 StreamReader.Close();//关闭读取器。
demo:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace MyPerson
{
public partial class FileStreamRW : Form
{
public FileStreamRW()
{
InitializeComponent();
}
private void btnRead_Click(object sender, EventArgs e)
{
string path, content;
path = txtPath.Text;
content = txtContent.Text;
if (String.IsNullOrEmpty(path) == true)
{
MessageBox.Show("路径不能为空");
return;
}
try
{
//创建文件流
FileStream myFs = new FileStream(path, FileMode.Open);
//创建读取器
StreamReader mySr = new StreamReader(myFs);
//读取文件所有的内容
content = mySr.ReadToEnd();
//将读取到的内容赋给txtCotent控件。
txtContent.Text = content;
//关闭读取器和文件流。
mySr.Close();
myFs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void btnWrite_Click(object sender, EventArgs e)
{
string path, content;
path = txtPath.Text;
content = txtContent.Text;
if (String.IsNullOrEmpty(path) == true)
{
MessageBox.Show("文件路径不能为空");
return;
}
try
{
//创建文件流
FileStream myFs = new FileStream(path, FileMode.Create);
//创建写入器
StreamWriter mySw = new StreamWriter(myFs);
//将敲击的内容写入文件中。
mySw.Write(content);
MessageBox.Show("写入成功!");
//关闭写入器和文件流。
mySw.Close();
myFs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
4、文件和目录操作:
1)File类和Directory类:
①:File类的方法:
| 方法 | 说明 |
| Exists(string path) | 检查指定的文件是否存在,返回bool值。 |
| Copy(string SourceFilePath , string DestinationFilePath) | 将指定path的源文件中内容复制到目标文件夹,如果目标文件夹不存在,则在指定路径中新建立一个文件。 |
| Move(string sourceFileName , string destFileName) | 将指定的文件移动到一个新的path。 |
| Delete(string path) | 删除指定的文件,如果指定的文件不存在,也不会发生异常。 |
②:Directory类的方法:
| 方法 | 说明 |
| Exists(string path) | 检查指定的文件是否存在。 |
| Move(string sourceDirName , string destFileName) | 将指定的文件移动到一个新的path。 |
| Delete(string , bool) | 删除指定的dir,如果为true,则删除子目录中的所有内容。 |
2)静态类与非静态类:
File类和Directory类在使用方法的时候不需要实例化,直接.方法名() 就可以使用。
静态类和非静态类的区别:
| 静态类 | 非静态类 |
| 用static 修饰 | 可以包含静态成员 |
| 只包含静态成员 | 可以包含静态成员 |
| 不可以包含实例成员 | 可以包含实例成员 |
| 使用类名调用静态成员 | 使用实例化对象调用非静态成员 |
| 不能被实例化 | 可以被实例化 |
| 不能包含实例构造函数 | 包含实例构造函数 |
P.S. “喜欢本文?现在就把它分享到 Twitter 或者通过RSS关注本站的更新 ^ O ^ 。” — by Angel. @ 2010-11-19 - 11:24。 转载请注明:来自 Angel's Blog.
本文永久链接: http://www.q86.net/2010/11/19/c-sharp-file-stream-read-and-write-file.html
按钮进行回复.
查看更多评论,请点击这里