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. (转) OpenLayers3基础教程——OL3 介绍control

    http://blog.csdn.net/gisshixisheng/article/details/46761535 概述: 本文讲述的是Ol3中的control的介绍和应用. OL2和OL3 co ...

  2. PS通道的界面颜色设置

    编辑--首选项---界面--界面---选项---(勾选)以彩色显示通道(彩色显示)或者不勾选(为黑白色显示)

  3. boost asio异步读写网络聊天程序客户端 实例详解

    boost官方文档中聊天程序实例讲解 数据包格式chat_message.hpp <pre name="code" class="cpp">< ...

  4. Linux基础:find命令总结

    本文只总结一些常用的用法,更详细的说明见man find和 info find. find命令 find命令常用来查找文件或目录,可以根据给定的路径和表达式查找所需的文件或目录.该工具是由findut ...

  5. C#第十节课

    类 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Thr ...

  6. jQuery(UI)常用插件

    jQuery 官方网站:http://jquery.com/ 下载地址:http://jquery.com/download/ 插件地址: http://plugins.jquery.com/ 常用插 ...

  7. hdu 1713求分数的最小公倍数

    题意中的圈数和天数说反了 #include<stdio.h> __int64 gcd(__int64 a,__int64 b) {/* 比如4/3 3/5 通分20/15 9/15 所以这 ...

  8. springcloud(一):Spring Cloud

    研究了一段时间Spring Boot了准备向Spring Cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...

  9. [Unit Testing] Set the timeout of a Test in Mocha

    Mocha uses a default timeout of 2000 ms. However, if for some reason that does not work for your use ...

  10. hdoj 1518 Square 【dfs】

    题意:给出n个(不同长度的)棍子,问能不能将他们构成一个正方形. 策略:深搜. hdoj 1455的简化版 代码: #include <stdio.h> #include <stri ...