首页 .NET/Web C# 【基础知识】读写txt记事本

C# 【基础知识】读写txt记事本

作者:胡同里的砖头 围观群众:169 更新于:2024-05-14

using System;
using System.IO;

namespace FileApplication
{
class Program
{
static void Main(string[] args)
{

string[] names = new string[] {"Zara Ali", "Nuha Ali"};
using (StreamWriter sw = new StreamWriter("names.txt"))
{
foreach (string s in names)
{
sw.WriteLine(s);

}
}

// 从文件中读取并显示每行
string line = "";
using (StreamReader sr = new StreamReader("names.txt"))
{
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
Console.ReadKey();
}
}
}
如果乱码,可以以下解决:
new StreamReader("C:/a.txt", Encoding.GetEncoding("GB2312"))
new StreamReader("c:/jamaica.txt", Encoding.UTF8)

  • 本文标题: C# 【基础知识】读写txt记事本
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8