当前位置:首页 > C#学习 > 正文内容

C# File类常用方法

小道6年前 (2018-12-03)C#学习3029

File类,是一个静态类,主要是来提供一些函数库用的。静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和打开一个文件。File类方法的参量很多时候都是路径path。File的一些方法可以返回FileStream和StreamWriter的对象。可以和他们配套使用。

System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间。

using System;
using System.IO;

namespace File类
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("创建一个新文档.txt.");//提示
            Console.ReadKey();//按任意键继续
            File.Create(@"D:\Users\Desktop\新文档.txt");//在指定路径中创建或覆盖文件。
            Console.WriteLine("TXT文本文档创建成功.");
            Console.ReadKey();
            Console.WriteLine("复制小道博客.txt并命名为:复制的文档.txt.");
            File.Copy(@"D:\Users\Desktop\小道博客.txt", @"D:\Users\Desktop\复制的文档.txt");//将现有文件复制到新文件。 不允许覆盖同名的文件。
            Console.WriteLine("复制文档成功.");
            Console.ReadKey();
            Console.WriteLine("删除小道博客.txt.");
            File.Delete(@"D:\Users\Desktop\小道博客.txt");//删除指定的文件。
            Console.WriteLine("删除文档成功.");
            Console.ReadKey();
            Console.WriteLine("将复制的文档.txt 移动到 TXT文件夹中.");
            File.Move(@"D:\Users\Desktop\复制的文档.txt", @"D:\Users\Desktop\TXT\复制的文档.txt");//将指定文件移到新位置,提供要指定新文件名的选项。
            Console.WriteLine("移动文档成功.");
            Console.ReadKey();
        }
    }
}

输出结果:

1.gif


File 读写操作.

using System;
using System.IO;
using System.Text;

namespace File读写
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入一段话:");//提示
            string str = Console.ReadLine();//输入
            byte[] b = Encoding.Default.GetBytes(str);//将指定字符串中的所有字符编码为一个字节序列。
            File.WriteAllBytes(@"D:\Users\Desktop\小道博客一.txt", b);//创建一个新文件,在其中写入指定的字节数组,然后关闭该文件。 如果目标文件已存在,则覆盖该文件。
            Console.WriteLine("写入成功!");
            Console.WriteLine("=================================================");
            Console.WriteLine("读取输出小道博客一.txt:");
            byte[] bRead = File.ReadAllBytes(@"D:\Users\Desktop\小道博客一.txt");
            string sRead = Encoding.Default.GetString(bRead);//将指定字节数组中的所有字节解码为一个字符串。
            Console.WriteLine(sRead);

            Console.WriteLine("=================================================");
            Console.WriteLine("请输入第一段话:");
            string[] strs = new string[2];
            strs[0] = Console.ReadLine();
            Console.WriteLine("请输入第二段话:");
            strs[1] = Console.ReadLine();
            for (int i = 0; i < strs.Length; i++)
            {
                File.WriteAllLines(@"D:\Users\Desktop\小道博客二.txt", strs,Encoding.Default);//创建一个新文件,在其中写入指定的字节数组,然后关闭该文件。
                Console.WriteLine("写入字符串 {0} 成功.", i);
            }
            Console.WriteLine("=================================================");
            Console.WriteLine("读取输出小道博客二.txt:");
            string[] strsRead = File.ReadAllLines(@"D:\Users\Desktop\小道博客二.txt",Encoding.Default);//打开一个文件,使用指定的编码读取文件的所有行,然后关闭该文件。
            foreach (var item in strsRead)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("=================================================");
            Console.WriteLine("请输入一段话:");
            str = Console.ReadLine();
            File.WriteAllText(@"D:\Users\Desktop\小道博客三.txt", str);//创建一个新文件,向其中写入指定的字符串,然后关闭文件。 如果目标文件已存在,则覆盖该文件。
            Console.WriteLine("=================================================");
            Console.WriteLine("读取输出小道博客三.txt:");
            Console.WriteLine(File.ReadAllText(@"D:\Users\Desktop\小道博客三.txt")); //打开一个文本文件,读取文件的所有行,然后关闭该文件。
            Console.ReadKey();
        }
    }
}

输出结果:

blob.png

扫描二维码推送至手机访问。

版权声明:本文由小道发布,如需转载请注明出处。

本文链接:https://daobk.com/post/131.html

分享给朋友:

“C# File类常用方法” 的相关文章

提示用户输入自己姓和名,最后输出姓名

提示用户输入自己姓和名,最后输出姓名

提示用户输入自己姓和名,最后输出姓名            Console.WriteLine("请输入您的姓");//提示用户输入他的姓。  &n...

while语句:用 while continue实现计算1到100之间的除了能被7整除之外所有整数的和。

while语句:用 while continue实现计算1到100之间的除了能被7整除之外所有整数的和。

while语句:用 while continue实现计算1到100之间的除了能被7整除之外所有整数的和。            int i = 1;/...

枚举enum

枚举enum

确定数量、确定值的几个取值:东西南北、男女、上中下。和用字符串比起来,用枚举的好处就是限定了变量的取值范围,程序处理起来更方便。namespace 枚举//命名空间 {     enum xb { 男, 女...

函数的ref、out参数

函数的ref、out参数

函数参数默认是值传递的,也就是“复制一份”ref必须先初始化,因为是引用,所以必须先“有”,才能引用,而out则是内部为外部赋值,所以不需要初始化,而且外部初始化也没用。ref应用场景内部对外部的值进行改变,out则是内部为外部变量赋值,out一般用在函数有多个返回值的场所。  ...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。