C#文件操作

7 篇文章 1 订阅

C#文件操作

File类

1.方法
(1)判存 File.Exist();
(2)复制 File.Copy();
(3)剪切 File.Move();
(4)删除 File.Delete();

2.文件读写
(1)字节读取
byte[] buffer = File.ReadAllByte(string path);
//将buffer以系统默认的编码格式转换成字符串
string text = System.Text.Encoding.Default.GetString(buffer);
(2)每行读取
string[] text = File.ReadAllLines(string path , Encoding.Default);
然后遍历输出。
(3)全部读取
string text = File.ReadAllText(string path , Encoding.Default);
(4)字节写入
byte[] buffer = System.Text.Encoding.Default.GetByte(text);
File.WriteAllBytes(string path , buffer);
(5)每行写入
string[] text = {" xxx " ," xxx ", " xxx ", " xxx "};
File.WriteAllLines(string path , text );
(6)全部写入
string text = “xxxxxxxxxxxxxxxxxxxx”;
File.WriteAllText(string path , text );
(7)追加写入
string text = “xxxxxxxxxxxxxxxxxxxx”;
File.AppendAllText(string path , text );

Directory类

1.常用方法
(1)从指定路径创建目录
CreateDirectory(String path);
(2)从指定路径删除空目录。
Delete(String path) ;
(3)删除指定的目录,并删除该目录中的所有子目录和文件
Delete(String, true)
(4)返回指定目录中文件的名称(包括其路径)。
GetFiles(String);
(5)将文件或目录及其内容移到新位置(剪切)。
Move(String path, String NewPath);

FileStream类

1. 常用方法
(1)读
using (FileStream fs = new FileStream(@“path”, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[fs.Length];
int r = fs.Read(buffer, 0, buffer.Length);
string text = System.Text.Encoding.Default.GetString(buffer);
}
(2)写
using (FileStream fs = new FileStream(@“path”, FileMode.OpenOrCreate, FileAccess.Write))
{
string text = “写入内容”;
byte[] buffer = System.Text.Encoding.Default.GetBytes(text);
fs.Write(buffer,0,buffer.Length);
}
(3)文件复制
private void FileCopy(string SourcePath,string TargetPath)
{
using (FileStream fsRead = new FileStream(SourcePath,FileMode.Open,FileAccess.Read))
{using (FileStream fsWrite = new FileStream(TargetPath,FileMode.OpenOrCreate,FileAccess.Write))
{
byte[] buffer = new byte[1024 * 1024 * 2];
while (true)
{
int r = fsRead.Read(buffer, 0, buffer.Length);
if (r==0)
{
break;
}
fsWrite.Write(buffer, 0, r);
}
}
}
}

StreamReader类 和 StreamWriter类

1. 读
(1)方法一
using (FileStream fsRead = new FileStream(@“C:\Users\suppo\Desktop\HTTP.txt”, FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fsRead))
{
while (!sr.EndOfStream)
{
text += sr.ReadLine() + “\r\n”;
textBox1.Text = text;
}
}
}
(2)方法二
using (StreamReader sr = new StreamReader(@“C:\Users\suppo\Desktop\HTTP.txt”))
{
while (!sr.EndOfStream)
{
text += sr.ReadLine() + “\r\n”;
textBox1.Text = text;
}
}
2. 写
(1)方法一
//FileMode.Append 追加写入 FileMode.Open覆盖写入
using (FileStream fsWrite = new FileStream(@“C:\Users\suppo\Desktop\HTTP.txt”, FileMode.Open, FileAccess.Write))
{
using (StreamWriter sr = new StreamWriter(fsWrite))
{
sr.WriteLine(text);
}
}
(2)方法2
//第二个参数为false是覆盖写入,为true是追加写入
using (StreamWriter sr = new StreamWriter(@“C:\Users\suppo\Desktop\HTTP.txt”,true))
{
sr.WriteLine(text);
}

注意:
File是操作小文件的。
FileStream 是操作字节的。大文件小文件都可以操作。
StreamReader类 和 StreamWriter类是操作大文件的。

C# 文件处理技术,包括file,fileinfo等,具体如下 第三章 文件处理技术 2 3-1 System.IO 命名空间 2 3-1-1 System.IO类介绍 2 3-1-2 File类的常用方法 4 3-1-3 Fileinfo类的常用方法 5 3-1 Fileinfo类的常用方法 5 1.案例学习:了解FileInfo类的一些主要属性 6 2.案例学习:实现文件的复制 6 3.案例学习:获取文件基本信息 8 3-2 文件夹类Directory的常用方法 10 1.案例学习:了解Directory类的一些主要方法 10 2.案例学习:获取文件的基本信息 11 3-3 File类的常用操作的静态方法练习 14 1.案例学习:简易文本编辑器的开发案例 15 3-4 文件流类FileStream 17 1.FileStream文件流类简介 17 2.FileStream文件流类的创建 18 3-4 文件读写例子 20 3-3-1案例学习:文件流FileStream综合案例(一) 20 3-5 文件流FileStream综合案例 30 3-3-2 案例学习:文件流FileStream综合案例(二) 30 3-6 读写二进制文件 33 3-6-1 二进制文件读取器/编写器介绍 33 3-7 写二进制文件案例学习 35 1. 案例学习:写二进制文件案例——图片的存储与复制 35 3-8 读写内存流 39 3-8-1 读写内存流——MemoryStream类 40 3-8-2 MemoryStream类案例学习 41 3-8-3 读写缓存流——BufferedStream类 43 3-9 读写缓存流 ——BufferedStream类 43 3-9-1 读写缓存流 ——BufferedStream类 43 3-9-2 BufferedStream类案例学习 43 1.案例学习:通过缓冲区交换数据 43 3-6本章小结 45
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值