C# 文件基本操作
概括的说,File,FileInfo,FileStream是用于文件 I/O 的类,StreamReader是用于从流读取和写入流的类,使用之前都需using System.IO。
先定义一个TXT文档路径: string txtpath = (@"D:\C#练习\1.txt"); 要读入这个文档。
(1)File 提供用于创建、复制、删除、移动和打开文件的静态方法,并协助创建 FileStream。
FileStream fs = File.Open(txtpath, FileMode.Open);
File可以直接调用各种方法(Open、Delete、Exists等)
例如: if (File.Exists(txtpath))
{
File.Delete(txtpath);
}
(2)FileInfo 提供用于创建、复制、删除、移动和打开文件的实例方法,并协助创建 FileStream。
FileInfo fi = new FileInfo(txtpath); //实例化
FileStream fs = fi.Open();
(3)FileStream 支持通过其 Seek 方法随机访问文件。默认情况下,FileStream 以同步方式打开文
件,但它也支持异步操作。
利用FileStream 我们可以得到一个文件的Streams,接着就是来读取。
(4)StreamReader 通过使用 Encoding 进行字符和字节的转换,从 Streams 中读取字符。
StreamWriter 通过使用 Encoding 将字符转换为字节,向 Streams 写入字符。
StreamReader sr = new StreamReader(fs);
string str = null;
string temp=null;
while((temp=sr.ReadLine())!=null)
{
str+=" "+temp;
}
得到一个字符串,再可以对字符串进行处理。
PS:
TextReader 是 StreamReader 和 StringReader 的抽象基类。抽象 Stream 类的实现用于字节输入和输出,而 TextReader 的实现用于 Unicode 字符输出。
TextWriter 是 StreamWriter 和 StringWriter 的抽象基类。抽象 Stream 类的实现用于字节输入和输出,而 TextWriter 的实现用于 Unicode 字符输出。
(5)directory 类
公开用于创建、移动和枚举通过目录和子目录的静态方法。 此类不能被继承。
using System;
using System.IO; namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string sourceDirectory = @"C:\current";
string archiveDirectory = @"C:\archive"; try
{
var txtFiles = Directory.EnumerateFiles(sourceDirectory, "*.txt"); foreach (string currentFile in txtFiles)
{
string fileName = currentFile.Substring(sourceDirectory.Length + 1);
Directory.Move(currentFile, Path.Combine(archiveDirectory, fileName));
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
//File类 , FileStream类
string path = @"e:\temp\MyTest.txt"; // Delete the file if it exists.
if (File.Exists(path))
{
File.Delete(path);
} //Create the file.
using (FileStream fs = File.Create(path))
{
AddText(fs, "This is some text");
AddText(fs, "This is some more text,");
AddText(fs, "\r\nand this is on a new line");
AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); for (int i = ; i < ; i++)
{
AddText(fs, Convert.ToChar(i).ToString()); }
} //Open the stream and read it back.
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b, , b.Length) > )
{
Console.WriteLine(temp.GetString(b));
}
}
} private static void AddText(FileStream fs, string value)
{
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, , info.Length);
}
最新文章
- CORS(跨源资源共享)实战
- Install Sublime Text 3
- Android安全开发之UXSS漏洞分析
- LLVM 笔记(一)—— phi 指令
- ASP.NET MVC IOC 之AutoFac攻略
- php学习笔记:foreach循环访问关联数组里的值
- phpcms—— 内容中的附件调用和添加远程地址的调用
- 转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文
- const形参和实参
- 利用cookies获取登录后的网页
- Android layoutInflate.inflate 方法具体解释,removeView()错误解决
- java.lang.Thread、java.lang.ThreadGroup和java.lang.ThreadLocal<;T>;详细解读
- Sublime Text、webstorm等编译器快速编写HTML/CSS代码的技巧
- 知识点补充 set 深浅拷贝
- 【Codeforces 1109C 】Sasha and a Patient Friend
- php 递归删除文件夹
- 【Properties】在Properties中配置List
- 解析Linux操作系统文件目录
- C++中,int a = 10的后面的操作
- 知名网站内部资料:WEB页面内容优化管理与性能技巧