操作文件方法简单总结(File,Directory,StreamReader,StreamWrite )
2013-09-13 11:29
501 查看
操作文件方法简单总结(File,Directory,StreamReader,StreamWrite )
对于文件夹,文件档的操作一直处于一知半解状态,有时间闲下来了,好好练习了一把,对文档,文件的操作有了一个基本的认知,若要深入了解,还是得通过实际的项目才行了,好了废话不多说,上酸菜!!
一 基本介绍
操作文档,文件夹,需要用到的类
1 Directory(静态类) : 用于创建、移动和删除等操作通过目录和子目录
DirectoryInfo(非静态):
2 File(静态类) :提供用于创建、复制、删除、移动和打开文件的静态类,并协助创建 FileStream 对象
FileInfo(非静态)
3 StreamReader:实现一个 TextReader,使其以一种特定的编码从字节流中读取字符
StreamWriter:实现一个 TextWriter,使其以一种特定的编码向流中写入字符
二 文件夹操作
操作文件夹用Directory 或者 DirectoryInfo
2.1 创建文件夹
展开代码
string path = @"F:\TestFile";
//创建文件夹
public void CreatFolder(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path);
}
Directory.CreateDirectory(path);
}
展开代码
2.2 删除文件夹
展开代码
string path = @"F:\TestFile";
//删除文件夹
public void DeleteFolder(string path)
{
//先删除文件夹中所有文件
DirectoryInfo directoryInfo = new DirectoryInfo(path);
foreach (var directory in directoryInfo.GetFiles())
{
File.Delete(directory.FullName);
}
//文件夹必须为空
if (Directory.Exists(path))
{
Directory.Delete(path);
}
}
展开代码
2.3 移动文件夹
展开代码
string path = @"F:\TestFile";
string newPath = @"F:\NewFile";
//移动文件夹
public void MoveFolder(string path, string newPath)
{
if (Directory.Exists(path))
{
//移动包括文件夹在内的所有文件
Directory.Move(path,newPath);
}
}
展开代码
2.4 获取文件夹下所有文件名
展开代码
string path = @"F:\TestFile";
//获取文件夹下所有文件名
public void GetFolderAllFiles(string path)
{
DirectoryInfo directory = new DirectoryInfo(path);
var files = directory.GetFiles();
foreach (var file in files)
{
Console.WriteLine(file.Name);
Console.WriteLine(file.FullName);//带文件路径全名
}
}
展开代码
三 文档操作(txt)
需要以下几个类:Directory 或者DirectoryInfo ,File 或者 FileInfo
3.1 创建文档
展开代码
string path = @"F:\TestFile\test.txt";
//创建文档
public void CreateFile(string path)
{
//文件路径用Directory判断是否存在
if (Directory.Exists(path))
{
//先删除存在的文件
if (File.Exists(path))
{
File.Delete(path);
}
File.Create(path);
}
}
展开代码
3.2 复制文档
View Code
string path = @"E:\TestFile\test.txt";
string newPath = @"E:\Test2File\newTest.txt";
//复制文档
public void CopyFile(string sourcePath, string destPath)
{
//只能针对同一卷轴(盘)下的文件
//destPath 为要复制的新地址,(destPath指定的文件名必须是未创建的,执行Copy方法时才会创建该文件)
if (File.Exists(sourcePath))
{
File.Copy(sourcePath, destPath);
}
}
3.3 移动文档
展开代码
string path = @"E:\TestFile\test.txt";
string newPath = @"E:\Test2File\newTest.txt";
//移动文档
public void MoveFile(string SourcePath, string destPath)
{
//只能针对同一卷轴(盘)下的文件
//destPath 为要移动的新地址(destPath指定的文件名必须是未创建的,执行Move方法时会将Source文件移动到新地址可以重新命名)
if (File.Exists(SourcePath))
{
File.Move(SourcePath, destPath);
}
}
展开代码
3.4 删除文档
展开代码
string path = @"E:\TestFile\test.txt";
//删除文档
public void DeleteFile(string path)
{
if (File.Exists(path))
{
File.Delete(path);
}
}
展开代码
四 文档读写操作(文档流操作) 对 第三点 的扩充
需要以下类:FileStream 此类继承了Stream 类并重写了大部分方法,并提供多个带参构造函数
4.1 写入字符
展开代码
string path = @"E:\TestFile\test.txt";
//往以有文档中写入字符
public void CreateText(string path)
{
StreamWriter writer=File.CreateText(path);
//直接写入字符的write方法,每次写入都会覆盖之前的内容
writer.Write("你好!");
//把缓存流的数据写入到基础流
writer.Flush();
//关闭StreamWriter对象及基础流
writer.Close();
}
展开代码
4.2 以流的形式写入文档
展开代码
//将一个文件内的数据,以流的方式读取,然后以流的形式写入到另一个文件中
public void ReadAndWriteFile()
{
string readPath = @"E:\TestFile\ReadStream.txt";
string writePath = @"E:\TestFile\WriteStream.txt";
//Read
//打开现有文件以进行读取
FileStream rs = File.OpenRead(readPath);
byte[] buffer = new byte[rs.Length];
rs.Read(buffer, 0, buffer.Length); //将文件内容读到字节流中
rs.Close();
//Write
//打开现有文件以进行写入
FileStream stream = File.OpenWrite(writePath);
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
stream.Close();
}
展开代码
4.3 以流的形式追加到文档
展开代码
//将一个文件内的数据,以流的方式读取,然后以流的形式写入到另一个文件中(追加内容)
public void ReadAndWriteNewFile()
{
string readPath = @"E:\TestFile\ReadStream.txt";
string writePath = @"E:\TestFile\WriteStream.txt";
//Read
FileStream rd = File.Open(readPath,FileMode.Open);
byte[] buffer = new byte[rd.Length];
rd.Read(buffer, 0, buffer.Length);
rd.Close();
//Write
FileStream wt = File.Open(writePath,FileMode.Append);//设为追加到原文件中
wt.Write(buffer,0,buffer.Length);
wt.Flush();
wt.Close();
}
展开代码
五 读写流操作文档
需要以下几个类:StreamReader StreamWriter
5.1 字符串读取写操作
展开代码
//读取文档并写入到另一文档中(用字符串写入的方法)
public void ReadFile()
{
string readPath = @"E:\TestFile\ReadStream.txt";
string writePath = @"E:\TestFile\WriteStream.txt";
using (StreamReader reader = new StreamReader(readPath,Encoding.Default)) //用默认的编码格式(必须要转格式否则乱码)
{
var rd = reader.ReadToEnd();
StreamWriter writer = new StreamWriter(writePath,true,Encoding.UTF8);//需要转成UTF-8的格式(可转可不转格式)
writer.Write(rd);
writer.Flush();
writer.Close();
}
}
展开代码
5.2 字符流读写操作
展开代码
//用字符流的方式读写文档
public void ReadWriteByByte()
{
string readPath = @"F:\TestFile\ReadStream.txt";
string writePath = @"F:\TestFile\WriteStream.txt";
using (StreamReader reader = new StreamReader(readPath,Encoding.Default))//需要指定编码,否则读到的为乱码
{
#region 错误方法
//Read 注意:文本中的字符只能被读取一次,第二次时读取不到了
//var readStr =reader.ReadToEnd();//第一次读取
//char[] buffer = new char[readStr.Length];
//reader.Read(buffer, 0, buffer.Length);//第二次读取时,读不到值
#endregion
//Read
char[] buffer = new char[10000];
reader.Read(buffer, 0, buffer.Length);
//Write
StreamWriter writer = new StreamWriter(writePath,true,Encoding.UTF8);
writer.Write(buffer, 0, buffer.Length);
writer.Flush();
writer.Close();
}
展开代码
六 总结
本文只对以上几个类常用的方法简单的介绍了,也是扫了下自己的盲区,如有更好的建议或方法,请指出。
另外如果觉得本文对你有一点小小的帮助不妨点下推荐,您的推荐是我写作的动力!!
相关文章推荐
- 操作文件方法简单总结(File,Directory,StreamReader,StreamWrite )(转载)
- 操作文件方法简单总结(File,Directory,StreamReader,StreamWrite )
- 操作文件方法简单总结(File,Directory,StreamReader,StreamWrite ) 转
- 操作文件方法简单总结(File,Directory,StreamReader,StreamWrite ) - Zery-zhang
- 操作文件方法简单总结(File,Directory,StreamReader,StreamWrite )
- 文件操作总结 (Path,Directory,File)
- C#对文件/目录的操作:Path、File、Directory、FileStream、StreamReader、StreamWriter等类的浅析
- Golang简单写文件操作的四种方法
- 利用 Hadoop FileSystem moveToLocalFile 方法下载文件 实现HDFS操作
- 【20150711】周总结【批量更新trick,UTF_FILE的文件操作,cur与循环
- C#基础精华04(文件流,文件操作,File、Directory、Path,Directory)
- Objective-c读取沙盒中文件的方法总结和NSFileManager主要操作
- java:File类中对文件或目录进行操作的常用方法以及遍历文件夹下面的文件
- C#文件与流(FileStream、StreamWriter 、StreamReader 、File、FileInfo、Directory、directoryInfo、Path、Encoding)
- java.io.File操作文件的一些常用方法
- Java 文件操作常用方法——File
- Android - 文件操作简单总结
- 简单的VC 操作XML 文件的的方法
- C#的File类中常用的文件操作函数(方法)及其使用
- File对文件的操作,创建、复制、删除、移动和打开文件的静态方法