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:	empMyTest.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, "
and this is on a new line");
                AddText(fs, "

The following is a subset of characters:
");

                for (int i = 1; i < 120; i++)
                {
                    AddText(fs, Convert.ToChar(i).ToString());

                }
            }

            //Open the stream and read it back.
            using (FileStream fs = File.OpenRead(path))
            {
                byte[] b = new byte[1024];
                UTF8Encoding temp = new UTF8Encoding(true);
                while (fs.Read(b, 0, b.Length) > 0)
                {
                    Console.WriteLine(temp.GetString(b));
                }
            }
        }

        private static void AddText(FileStream fs, string value)
        {
            byte[] info = new UTF8Encoding(true).GetBytes(value);
            fs.Write(info, 0, info.Length);
        }
原文地址:https://www.cnblogs.com/youguess/p/4630575.html