C# 文件基本操作

本文详细介绍了C#中的文件I/O操作,包括使用File、FileInfo、FileStream类进行文件的创建、读取、写入及删除等基本操作。同时,还介绍了如何使用StreamReader和StreamWriter类来处理文本文件,以及如何利用Directory类进行目录的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概括的说,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 = 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值