C#控制台进行文件读写的方法

前端技术 2023/09/03 C#

本文实例讲述了C#控制台进行文件读写的方法。分享给大家供大家参考。具体如下:

C#控制台进行文件写入:

using System;
using System.IO;
class file1 {
  public static void Main()
  {
    string PATH = null;
    Console.WriteLine(\"file path: \");
    //这里输入文本所在目录 例如 d:\\text.txt
    PATH = Console.ReadLine();
    StreamWriter sw = new StreamWriter(PATH);
    string str = null;
    while(true){
      str = Console.ReadLine();
      if (str == \"end\") {
        break;
      }
      sw.WriteLine(str);
    }
    sw.Close(); 
    Console.ReadKey();
  }
}

C#控制台进行文件读取

using System;
using System.IO;
class file1 {
  public static void Main() {
    string PATH = null;
    Console.WriteLine(\"file path: \");
    //这里输入文本所在目录 例如 d:\\text.txt
    PATH = Console.ReadLine();
    StreamReader r = File.OpenText(PATH);
    while(!r.EndOfStream){
      Console.WriteLine(r.ReadLine());
    }
    Console.ReadKey();
  }
}

希望本文所述对大家的C#程序设计有所帮助。

本文地址:https://www.stayed.cn/item/8135

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。