Directory文件类,File,FielStream、StreamRead和StreamWriter的使用

(转载)

创建一个新文件
Directory.CreateDirectory(@"C: \Users\enle\Desktop\new");//路径
删除一个文件
Directory.Delete(@"C: \Users\enle\Desktop\new", true);
剪切一个文件
Directory.Move(@"C:\Users\enle\Desktop\new", @"C:\Users\enle\Desktop\new1");
读取文件所有文件的全路径
string[] path = Directory.GetFiles(@"C:\Users\enle\Desktop\2", "*.pdf");//"*.pdf" 对读取文件的格式,进行限定
for (int i = ; i < path.Length; i++)
{
Console.WriteLine(path[i]);
}
文件夹的路径
string[] path = Directory.GetDirectories(@"C:\Users\enle\Desktop\2"); for (int i = ; i < path.Length; i++)
{
Console.WriteLine(path[i]);
}
           Console.ReadKey()
创建文件夹
if (Directory .Exists (@"C:\Users\enle\Desktop\new1"))
{
for (int i = ; i < ; i++)
{
Directory.CreateDirectory(@"C: \Users\enle\Desktop\new1\" + i); }
}

//读取文本文件

            // FileMode.OpenOrCreate你针对文件进行一个什么操作
//FileAccess .Read 文件数据参数
//1.创建FileStream对象
FileStream fileRead = new FileStream(@"C:\Users\wbrm\Desktop\新建文本文档.txt", FileMode.OpenOrCreate, FileAccess.Read);
byte[] buffer = new byte[ * * ];
int r = fileRead.Read(buffer, , buffer.Length); // 0表示从那个字节读取数据 // buffer.Length 读取的大小
//将字节数组中的每一个元素安照知道的编码格式解码成字符串
string s = Encoding.Default.GetString(buffer, , r);
fileRead.Close(); // //关闭流
fileRead.Dispose(); // //释放流所占用的资源
 写入文本文件
            //写入的编码格式和读的编码格式必须一样
//将创建的文件流对象的过程在Using当中,会自动的帮助我们释放所占用的空间
using (FileStream fswrite = new FileStream(@"C:\Users\wbrm\Desktop\新建文本文档 (2).txt", FileMode.OpenOrCreate, FileAccess.Write))
{
string str = "看我有没有把你覆盖掉";
byte[] buffer = Encoding.Default.GetBytes(str);//转换成字节数组
fswrite.Write(buffer, , buffer.Length);
}

使用文件流实现多媒体文件文件的复制

static void Main(string[] args)
{
//思路:先将要复制的多媒体文件读取出来然后在再写到你的指定位置
string courece = @"C:\Users\wbrm\Desktop\for循环的练习.avi";
string target = @"C:\Users\wbrm\Desktop\新的.avi";
CopyFile(courece, target);
}
public static void CopyFile(string courece, string target)
{
//读取流
using (FileStream fsRead = new FileStream(courece, FileMode.OpenOrCreate, FileAccess.Read))
{
//写入流
using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = new byte[ * * ];
//因为文件可能会比较大,所以我们在读取的时候,应该通过一个循环去读取
while (true)
{
//返回本次读取实际读取的字节数
int r = fsRead.Read(buffer, , buffer.Length);
//如果返回一个0,也就意味什么都没有读取到,读取完了
if(r==)
{
break;
}
fsWrite.Write(buffer, ,r);//最多写入的字节r
}
}
}
}
//StreamRead来读取一个文件
using (StreamReader sr = new StreamReader(@"C:\Users\enle\Desktop\新建文本文档.txt", Encoding.Default))
{
while (!sr.EndOfStream)//循环的去读文件
{
var str=sr.ReadLine();
}
}
//StreamWriter写入一个文本文件
using (StreamWriter strw = new StreamWriter(@"C:\Users\wbrm\Desktop\新建.txt",true ))//true 表示追加
{
strw.Write("哈哈哈");
}

文件操作:Directory,File,FielStream、StreamRead和StreamWriter的使用的更多相关文章

  1. Python:文件操作技巧(File operation)(转)

    Python:文件操作技巧(File operation) 读写文件 # ! /usr/bin/python #  -*- coding: utf8 -*- spath = " D:/dow ...

  2. C#基础精华04(文件流,文件操作,File、Directory、Path,Directory)

    文件流 FileStream  可读可写  大文件  释放 StreamReader 读取   释放 StreamWriter 写入   释放 using 中释放 File 可读可写  小文件 操作文 ...

  3. 文件操作类File

    File类:提供用于创建.复制.删除.移动和打开文件的静态方法.File类 FileInfo类:提供创建.复制.删除.移动和打开文件的属性和实例方法.FileInfo类 Directory类:公开用于 ...

  4. 流的文件操作(File)

    一.流的分类: 1.流按照方向分类:分为输入流和输出流,流的操作是相对于内存而言. 输入流的定义:当我们从数据源中将数据读取到内存中就称为输入流,也叫读取流. 输出流的定义:当我们将内存中处理好的数据 ...

  5. Python基础【第十一篇】文件操作(file()、open()方法和fileinput模块)

    一.file/open 内置函数 file函数的方法: 注:file 和 open的用法和功能相同这里只对file进行分析 file(‘filename’,’mode’) file(‘filename ...

  6. 文件操作(File类等)API摘要

    Console 此类包含多个方法,可访问与当前 Java 虚拟机关联的基于字符的控制台设备(如果有). 虚拟机是否具有控制台取决于底层平台,还取决于调用虚拟机的方式.如果虚拟机从一个交互式命令行开始启 ...

  7. 文件操作之File 和 Path

    转载:https://blog.csdn.net/u010889616/article/details/52694061 Java7中文件IO发生了很大的变化,专门引入了很多新的类: import j ...

  8. java的文件操作类File

    java.io.File类,是java获取文件/文件夹的所有属性,和完成所有相关操作的类 例子: package test.file.IO; import java.io.*; public clas ...

  9. java 基础 —— 文件操作(File)

    1. 基本成员: File.separator public class File implements Serializable, Comparable<File> { private ...

随机推荐

  1. Kind (type theory)-higher-kinded types--type constructor

    , pronounced "type", is the kind of all data types seen as nullary type constructors, and ...

  2. windows 下安装mysql 成功版

    mysql 下载地址 http://dev.mysql.com/downloads/ zip版下载 解压到本地 假设文件保存在C:\mysql-5.7.17-winx64 1.以管理员身份运行cmd. ...

  3. vue 登录验证码

    vue 登录验证码 最近在开发pc端项目,配合elementui使用 createCode() { var code = ""; var codeLength = 4; //验证码 ...

  4. SYN 和 RTO

    转自:https://huoding.com/2017/08/13/628 前两天,我在微博上推荐了一篇朝花夕拾的文章:The story of one latency spike,文章中介绍了 cl ...

  5. eclipse导入Javaweb文件出错解决

    在项目名上右击打开properties,如图在箭头指的位置可以看出有个unbound表示导入的资源库出现 异常,需要手动导入,1.点击Server Library{Apache Tomcat v9.0 ...

  6. Redis启动出错 noauth authentication required.

    输入认证过的密码即可. 在命令行中运行: auth password

  7. (25)Spring Boot使用自定义的properties【从零开始学Spring Boot】

    spring boot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,我们应该怎么做呢. 若继续在application.properties中添加 如: ...

  8. (9)使用JdbcTemplate【从零开始学Spring Boot】

    整体步骤: (1)   在pom.xml加入jdbcTemplate的依赖: (2)   编写DemoDao类,声明为:@Repository,引入JdbcTemplate (3)   编写DemoS ...

  9. 0926mysql中MRR的用法

    转自 http://blog.itpub.net/22664653/viewspace-1673682  [MySQL]MySQL5.6新特性之Multi-Range Read 2015-05-27 ...

  10. 输入url发生了什么--前端所有知识

    面试经常会问到的一个问题,这个问题舒展开来,其实包含了前端(一些后端)几乎所有的知识.梳理一下,备忘.包含了一些面经中常问的问题. 有时间待续