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. monad - the Category hierachy

    reading the "The Typeclassopedia" by Brent Yorgey in Monad.Reader#13 ,and found that " ...

  3. 静态修改url,不跳转

    history.replaceState(null,document.title,'www.baidu.com');

  4. jquery相关常用的工具函数

    1.弹出提示框: function prompt(msg){ $("<div>" + msg + "</div>").css({ &qu ...

  5. java操作Excel的poi 创建一个sheet页

    package com.java.poi; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.us ...

  6. matlab学习下拉菜单

    用matlab添加listbox控件 修改string和value值,value为几就对应第几行字符串 添加button按钮,将string值改为“选择x轴参数”,字体大小为10 再添加一个按钮,将s ...

  7. python从TXT创建PDF文件——reportlab

    使用reportlab创建PDF文件电子书一般都是txt格式的,某些电子阅读器不能读取txt的文档,如DPT-RP1.因此本文从使用python实现txt到pdf的转换,并且支持生成目录,目录能够生成 ...

  8. 洛谷P1141 01迷宫【DFS】

    有一个仅由数字00与11组成的n \times nn×n格迷宫.若你位于一格0上,那么你可以移动到相邻44格中的某一格11上,同样若你位于一格1上,那么你可以移动到相邻44格中的某一格00上. 你的任 ...

  9. Linux—Ubuntu14.0.5 修改gitlab管理员的密码

    1. 在root用户下,执行 gitlab-rails console production #进入gitlba控制台 2.获得用户数据,修改用户密码 [root@svr34 bin]# gitlab ...

  10. 26.mget批量查询

    主要知识点     一.mget批量查询的好处     get查询就是一条一条的查询,比如说要查询100条数据,那么就要发送100次网络请求,这个开销还是很大的.如果使用mget进行批量查询的话,查询 ...