一 基本介绍

操作文档,文件夹,需要用到的类

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 复制文档

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, , buffer.Length); //将文件内容读到字节流中
rs.Close();
//Write
//打开现有文件以进行写入
FileStream stream = File.OpenWrite(writePath);
stream.Write(buffer, , 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, , buffer.Length);
rd.Close(); //Write
FileStream wt = File.Open(writePath,FileMode.Append);//设为追加到原文件中
wt.Write(buffer,,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[];
reader.Read(buffer, , buffer.Length);
//Write
StreamWriter writer = new StreamWriter(writePath,true,Encoding.UTF8);
writer.Write(buffer, , buffer.Length);
writer.Flush();
writer.Close();
}

操作文件方法简单总结(File,Directory,StreamReader,StreamWrite ) - Zery-zhang的更多相关文章

  1. 操作文件方法简单总结(File,Directory,StreamReader,StreamWrite )

    对于文件夹,文档的操作一直处于一知半解状态,有时间闲下来了,好好练习了一把,对文档,文件的操作有了一个基本的认知, 若要深入了解,还是得通过实际的项目才行了,好了废话不多说,上酸菜!! 注:红色标题为 ...

  2. 操作文件方法简单总结(File,Directory,StreamReader,StreamWrite )(转载)

    本文转自http://www.cnblogs.com/zery/p/3315889.html 对于文件夹,文档的操作一直处于一知半解状态,有时间闲下来了,好好练习了一把,对文档,文件的操作有了一个基本 ...

  3. 【Python】 文件和操作文件方法

    文件 ■ 基本的文件用法 f = open("path","mode") mode有a,w,r,b,+等.默认为r.模式与打开文件时的动作有关系,比如用w打开的 ...

  4. linq操作文件方法

    备忘 string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); List<File ...

  5. 【Android Studio 小技巧】一键查看文件方法结构目录File Structure

    看源代码的时候,如果可以查看class中的所有方法,可以提高效率.Android Studio 中可以使用快捷键一键显示所有方法的目录. Mac: command + fn + F12 (在mac中的 ...

  6. 使用File类、StreamRead和StreamWrite读写数据、以及Path类操作文件路径和Directory

    1.File类的概念: File类,是一个静态类,主要是来提供一些函数库用的.静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和 打开一个文件. File类方法的参量 ...

  7. java File文件操作共用方法整理

    package org.jelly.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io ...

  8. .net(C#)操作文件的几种方法汇总

    .net(C#)操作文件的几种方法汇总 System.IO命名空间下类的用法:在System.IO名称空间中包含了用于文件输入输出的主要类.File:实用类,提供许多静态方法,用于移动.复制和删除文件 ...

  9. C#操作文件夹及文件的方法的使用

    本文收集了目前最为常用的C#经典操作文件的方法,具体内容如下:C#追加.拷贝.删除.移动文件.创建目录.递归删除文件夹及文件.指定文件夹下面的所有内容copy到目标文件夹下面.指定文件夹下面的所有内容 ...

随机推荐

  1. php单链表实现

    php单链表实现 <?php //单链表 class Hero{ public $no; public $name; public $nickname; public $next=null; f ...

  2. Dynamics CRM 2011 通过工作流发邮件时的权限问题

    场景: 在CRM中配置工作流,完成某个步骤后,发送邮件通知其他用户.发件人统一配置为管理员,收件人则根据业务需要设定动态值. 相关权限配置 首先启动流程的用户, 需要允许其他用户代表发送电子邮件 另外 ...

  3. php使用substr中文乱码问题

    周天的时候对网站 https://www.javasec.cn 进行bug修复和功能更新,其中遇到一个比较有意思的小问题: 问题: 网站的置顶推荐中,有文本略缩.但是无论怎么修改最后一个字符始终现实为 ...

  4. Shell编程的基本语法

    Shell编程 创建sh文件 touch test.sh vim test.sh 写入如下内容 #!/bin/bash a="hello" 运行 chmod +x /root/te ...

  5. Python re模块与正则表达式的运用

    re模块 永远不要起一个py文件的名字,这个名字和你已知的模块同名 查找 findall():     匹配所有   每一项都是列表中的一个元素 语法 :   findall(正则判断条件,要判断字符 ...

  6. AMFObject 数据格式浅析

    amf.h中关于 AMFObject 是这样的定义的: typedef struct AMFObject { int o_num; struct AMFObjectProperty *o_props; ...

  7. Spring Cloud Eureka 1(eureka简介)

    Spring Cloud Eureka 是 Spring Cloud Netflix微服务套件中的一部分,基于netflix eureka做了二次封装,主要负责完成微服务架构中的服务治理功能. 服务治 ...

  8. mysql之提示符

    MySQL常用的简单的命令: mysql> PROMPT \u@\h \d> PROMPT set to '\u@\h \d>' root (none)>USE test Da ...

  9. python中descriptor的应用

    [python中descriptor的应用] 1.classmethod. 1)classmethod的应用. 2)classmethod原理. 2.staticmethod. 1)staticmet ...

  10. sql2012增加Sequence对象

    官方给出了一大堆SQL2012相对于SQL2008R2的新特性,但是大多数对于普通开发人员来说都是浮云,根本用不到,下面就说说一些对于开发人员来说比较有用的新特性. Sequence对象对于Oracl ...