C#文件读写操作
1 文件和目录操作
.NET框架类库向用户提供了一个File类和Directory类,它们位于System.IO命名空间中。
1.1 File类的方法
方法 | 说明 |
Create (string path) | 在指定路径中创建或覆盖文件 |
Exists(string path) | 用于检查指定文件是否存在,该方法返回一个布尔值 |
Copy(string SourceFilePath, string DestinationFilePath) | 按指定路径的源文件中的内容复制到目标文件中,如果目标 文件不存在,则在指定路径中新建一个文件 |
Move (stringsourceFileName, | 将指定文件移动到一个新的路径 |
Delete(string path) | 删除指定的文件,如果指定的文件不存在,则不引发异常 |
1.2 Directory类的方法
方法 | 说明 |
CreateDirectory (string path) | 按 path 的指定创建所有目录和子目录。 |
Exists(string path) | 用于检查指定文件夹在磁盘上是否存在 |
Move(stringsourceDirName, stringdestDirName) | 用于将文件或目录及其内容移到新位置 |
Delete(string,Boolean) | 删除指定目录,如果bool指定true,则删除子目录中的所有目录内容 |
Delete(string path) | 删除指定的文件,如果指定的文件不存在,则不引发异常 |
1.3 示例:创建目录和文件
public string _directory = @"D:\MyDir\"; //目录
public string _fileName = "MyFile.txt"; //文件名
/// <summary>
/// 创建目录与文件
/// </summary>
public void CreateFile()
{
try
{
string path = _directory + _fileName;
if (!Directory.Exists(_directory))
{
Directory.CreateDirectory(_directory);
}
if (!File.Exists(path))
{
File.Create(path);
}
}
catch (Exception e)
{
Response.Write(e.StackTrace);
}
}
2 文件的读写操作
2.1 使用StreamReader和StreamWriter读写文件
2.1.1 使用StreamWriter写入文件
public void WriteMessage()
{
string path = _directory + _fileName;
string message = "我的博客:http://blog.csdn.net/pan_junbiao/";
try
{
//如果文件不存在,则创建文件
if (!File.Exists(path))
{
CreateFile();
}
//1.创建文件流
FileStream fileStream = new FileStream(path, FileMode.Create);
//2.创建写入器
StreamWriter streamWriter = new StreamWriter(fileStream);
//3.将内容写入文件
streamWriter.WriteLine(message);
//4.关闭写入器
streamWriter.Close();
//5.关闭文件流
fileStream.Close();
}
catch (Exception e)
{
Response.Write(e.StackTrace);
}
}
2.1.2 使用StreamReader读出文件
public void ReadMessage()
{
string path = _directory + _fileName;
string message = "";
try
{
//判断文件是否存在
if (File.Exists(path))
{
//1.创建文件流
FileStream fileStream = new FileStream(path, FileMode.Open);
//2.创建读取器
StreamReader streamReader = new StreamReader(fileStream);
//3.读取文本所有内容
message = streamReader.ReadToEnd();
//4.关闭读取器
streamReader.Close();
//5.关闭文件流
fileStream.Close();
//输出内容
Response.Write(message);
}
}
catch (Exception e)
{
Response.Write(e.StackTrace);
}
}
2.2 使用缓存流复制文件
2.2.1 示例:将歌曲一个目录下复制到另一目录下,并重命名。
/// <summary>
/// 使用缓存流复制文件
/// </summary>
public void BufferedOperation()
{
string path1 = @"D:\MyMusic1\Music.mp3";
string path2 = @"D:\MyMusic2\Music_bet.mp3";
try
{
//判断文件是否存在
if (File.Exists(path1))
{
//创建两个文件流 一个是源文件相关,另一个是要写入的文件
FileStream fileStream1 = new FileStream(path1, FileMode.Open);
FileStream fileStream2 = new FileStream(path2, FileMode.Create);
//创建一个字节数组,作为两者之间的媒介
byte[] data = new byte[1024];
//创建两个缓冲流,与两个文件流相关联
BufferedStream bufferedStream1 = new BufferedStream(fileStream1);
BufferedStream bufferedStream2 = new BufferedStream(fileStream2);
//复制文件
while (fileStream1.Read(data, 0, data.Length) > 0)
{
fileStream2.Write(data, 0, data.Length);
fileStream2.Flush();
}
//关闭文件流
fileStream1.Close();
fileStream2.Close();
}
}
catch (Exception e)
{
Response.Write(e.StackTrace);
}
}
还没有评论,来说两句吧...