当前位置:   article > 正文

C#操作文件流,读取和写入文件_c# 获取文件流

c# 获取文件流

1.Hello,各位码友们大家好,最近遇到了一些问题,大致就是将生成的字符串写入一个项目文件中的一个记事本中,其次就是读取记事本中的内容,并且清空整个记事本中的内容,主要涉及的是,c#中关于文件流的操作,读取和写入文件,接下来,给大家分享一些知识点,有错误的地方还请各位码友们斧正!

2.首先写认识C#中的一个命名空间,操作文件流的时候需要使用它,“System.IO”,它是C#中用于输入和输出操作的命名空间。它提供了许多类和方法,用于处理文件、文件夹和流的读取和写入操作,以下是该命名空间常用的类以及对应的功能。

File类:用于创建、复制、移动、删除和读写文件等操作。它包含一组静态方法,例如Create、Copy、Move、Delete、ReadAllText、WriteAllText等。
Directory类:用于创建、复制、移动和删除文件夹等操作。它包含一组静态方法,例如CreateDirectory、Copy、Move、Delete等。
Path类:用于处理文件和文件夹路径的常见操作。它包含一组静态方法,例如Combine(用于组合路径)、GetExtension(获取文件扩展名)、GetFileName(获取文件名)等。
FileStream类:用于在文件中进行读取和写入操作的流。它允许你以字节为单位读取和写入文件内容。
StreamReader和StreamWriter类:用于以文本方式读取和写入文件内容的流。它们提供了更方便的方法,如逐行读取和写入文本数据。
BinaryReader和BinaryWriter类:用于以二进制格式读取和写入文件内容的流。它们允许你以字节为单位读取和写入数据,适用于处理

3.清除C#项目中文件的内容方法一:

使用File.WriteAllText()方法,通常用于将一些文本写入文件,要清除文件的内容,您只需使用以下命令将空文本写入文件即可以项目文件中的一个记事本为例子,代码如下:
  1. using System;
  2. using System.IO;
  3. public class Example
  4. {
  5. public static void Main()
  6. {
  7. string path = @"D:\Test\data.txt";
  8. File.WriteAllText(path, string.Empty);
  9. }
  10. }

4.清除C#项目中文件的内容方法二:

使用FileStream.SetLength()方法,创建一个新的文件流,使用FileStream.SetLength() 方法,最后冲洗流。
  1. using System;
  2. using System.IO;
  3. public class Example
  4. {
  5. public static void Main()
  6. {
  7. string path = @"D:\Test\data.txt";
  8. using (FileStream fs = new FileStream(path, FileMode.Open))
  9. {
  10. fs.SetLength(0);
  11. }
  12. }
  13. }
其实,可以直接打开流 FileMode.Truncate 模式,然后关闭它。这将清除文件的内容,甚至不必调用 FileStream.SetLength() 方法。
  1. using System;
  2. using System.IO;
  3. public class Example
  4. {
  5. public static void Main()
  6. {
  7. string path = @"D:\Test\data.txt";
  8. using (FileStream fs = new FileStream(path, FileMode.Truncate)) {
  9. }
  10. }

6.使用 File.Create() 方法

最后,您可以使用 File.Create() 方法,如果文件不存在,则在指定路径创建一个文件。如果该文件确实存在,并且不是只读的,则内容将被覆盖。
  1. using System;
  2. using System.IO;
  3. public class Example
  4. {
  5. public static void Main()
  6. {
  7. string path = @"D:\Test\data.txt";
  8. File.Create(path).Close();
  9. }
  10. }

5.C# 读取和写入文本文件

     C#读取文件:
  1. //Read a Text File 读取一个文本文件
  2. using System;
  3. using System.IO;
  4. namespace readwriteapp
  5. {
  6. class Class1
  7. {
  8. [STAThread]
  9. static void Main(string[] args)
  10. {
  11. String line;
  12. try
  13. {
  14. //将文件路径和文件名传递给StreamReader构造函数
  15. StreamReader sr = new StreamReader(@"D:\Test\data.txt");
  16. //读取文本的第一行
  17. line = sr.ReadLine();
  18. //继续读取,直到到达文件末尾
  19. while (line != null)
  20. {
  21. //将行写入控制台窗口
  22. Console.WriteLine(line);
  23. //读取下一行
  24. line = sr.ReadLine();
  25. }
  26. //关闭文件:将已打开的文件关闭,以便释放系统资源或进行其他操作。
  27. sr.Close();
  28. Console.ReadLine();
  29. }
  30. catch(Exception e)
  31. {
  32. Console.WriteLine("Exception: " + e.Message);
  33. }
  34. finally
  35. {
  36. //执行最后一个块
  37. Console.WriteLine("Executing finally block.");
  38. }
  39. }
  40. }
  41. }
    C#写入文件:Version1
  1. //Write a text file Version1
  2. using System;
  3. using System.IO;
  4. namespace readwriteapp
  5. {
  6. class Class1
  7. {
  8. [STAThread]
  9. static void Main(string[] args)
  10. {
  11. try
  12. {
  13. //将文件路径和文件名传递给StreamWriter构造函数
  14. StreamWriter sw = new StreamWriter(@"D:\Test\data.txt");
  15. //写第一行文字
  16. sw.WriteLine("Hello World!!");
  17. //写第二行文字
  18. sw.WriteLine("From the StreamWriter class");
  19. //关闭文件
  20. sw.Close();
  21. }
  22. catch(Exception e)
  23. {
  24. Console.WriteLine("Exception: " + e.Message);
  25. }
  26. finally
  27. {
  28. Console.WriteLine("Executing finally block.");
  29. }
  30. }
  31. }
  32. }
    C#写入文件:Version2
  1. //Write a text file - Version2
  2. using System;
  3. using System.IO;
  4. using System.Text;
  5. namespace readwriteapp
  6. {
  7. class Class1
  8. {
  9. [STAThread]
  10. static void Main(string[] args)
  11. {
  12. Int64 x;
  13. try
  14. {
  15. //打开文件
  16. StreamWriter sw = new StreamWriter(@"D:\Test\data.txt",true,Encoding.ASCII);
  17. //把数字1到10写在同一行上。
  18. for(x=0; x < 10; x++)
  19. {
  20. sw.Write(x);
  21. }
  22. //关闭文件
  23. sw.Close();
  24. }
  25. catch(Exception e)
  26. {
  27. Console.WriteLine("Exception: " + e.Message);
  28. }
  29. finally
  30. {
  31. Console.WriteLine("Executing finally block.");
  32. }
  33. }
  34. }
  35. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/913768
推荐阅读
相关标签
  

闽ICP备14008679号