c# 读写文件

读写文件需要导入库(using System.IO;)。
1.写文件
方法一(使用FileStream):
string filepath = @“D:\test.txt”;
if (File.Exists(filepath))//判断文件是否存在
{
// FileMode.Append: 如果文件存在则在末尾添加,文件不存在则新建
FileStream fs = File.Open(filepath, FileMode.Append);//打开文件且在文件末尾添加
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(“test”);//在文件末尾新增一行添加test
sw.Close();
fs.Close();
}
else
{
//FileMode.CreateNew–文件若存在则报异常
//FileMode.Create–文件若存在也不会报异常,但文件会被重写
FileStream f1 = File.Open(filepath, FileMode.CreateNew, FileAccess.Write);//文件不存在,新建文件在指定路径
StreamWriter sw = new StreamWriter(f1);
sw.WriteLine(“aaa”);//在文件末尾新增一行添加aaa
sw.Close();
f1.Close();
}
方法二(使用File.WriteAllText,仅写入一个字符串):
string filepath = @“D:\test.txt”;
string text = “aaa”;
File.WriteAllText(filepath, text);//如果路径不存在,会新建;如果已存在,则覆盖。给d盘的test.txt写入“aaa”
File.WriteAllText(filepath, text, Encoding.UTF8);//如果路径不存在,会新建;如果已存在,则覆盖。给d盘的test.txt写入“aaa”
方法三(使用File.WriteAllLines,写入指定的字符串数组,多行显示):
string filepath = @“D:\test.txt”;
string[] strs = { “Good Morning!”, “Good Afternoon!” };//需要写入的字符串数组
File.WriteAllLines(filepath, strs);//如果路径不存在,会新建;如果已存在,则覆盖。给d盘的test.txt写入strs
File.WriteAllLines(filepath, strs, Encoding.UTF8);///如果路径不存在,会新建;如果已存在,则覆盖。给d盘的test.txt写入strs
方法四(使用StreamWriter)
StreamWriter sw1 = new StreamWriter(filepath); // 如果文件不存在,创建文件; 如果存在,覆盖文件
sw1.WriteLine(“line1”);//写入第一行
sw1.WriteLine(“line2”);//写入第二行
sw1.Close();
StreamWriter sw2 = new StreamWriter(filepath, true, Encoding.UTF8);//true是append text, false为覆盖原文件.文件如果不存在则新建。 Encoding.UTF8是指定编码方式
sw2.WriteLine(“line1”);//写入第一行
sw2.WriteLine(“line2”);//写入第二行
sw2.Close();
2 读文件
方法一(使用StreamReader按行读)
string filepath = @“D:\test.txt”;
StreamReader objReader = new StreamReader(filepath);//filepath是要读取的文件的路径
string strLine1 = objReader.ReadLine();//读取文件的第一行写入strLine1
while (strLine1 != null)//按行读到文件末尾停止循环
{
Console.WriteLine(strLine1);//在控制台打印出来
strLine1 = objReader.ReadLine();//继续读取下一行
}
objReader.Close();//关闭读文件
方法二(File.ReadAllText一次性读完)
string str1 = File.ReadAllText(filepath);//全部读完存入str1
string str2 = File.ReadAllText(filepath, Encoding.UTF8);//全部读完存入str2
方法三(File.ReadAllLines一次性读完)
string[] strs1 = File.ReadAllLines(filepath); //全部读完按行存入数组strs1
string[] strs2 = File.ReadAllLines(filepath, Encoding.UTF8);//全部读完按行存入数组strs2
方法四(使用FileStream按行读)
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read); //初始化FileStream,文件不存在会报错
StreamReader sr = new StreamReader(fs); //初始化StreamReader
string strLine1 = sr.ReadLine();//读取文件的第一行写入strLine1
while (strLine1 != null)//按行读到文件末尾停止循环
{
Console.WriteLine(strLine1);//在控制台打印出来
strLine1 = sr.ReadLine();//继续读取下一行
}
sr.Close();//关闭读文件
fs.Close();//关闭

3.常用的分隔正则表达式
使用正则表达式需要导入库using System.Text.RegularExpressions;
string temp=“aaa_bbb_ccc_ddd.txt”;
temp=temp.Replace(“.txt”,“”);//将temp里面的".txt"替换成""
string[] end = Regex.Split(temp, “_”, RegexOptions.IgnoreCase);//将temp按下划线分隔成数组end
foreach(string e in end)//使用foreach循环
{
Console.WriteLine(e);//将end里面的元素依次打印在控制台
}
运行结果
在这里插入图片描述
4.Trim用法
Console.Write(“start”);
string temp=" aaa ";
string aa = temp.Trim();//Trim()移除头尾的空白符
string bb = temp.TrimStart();//Trim()移除头部的空白符
string cc = temp.TrimEnd();//Trim()移除尾部的空白符
Console.WriteLine(aa + “||” + bb + “||” + cc + “||” + “end”);

运行结果
在这里插入图片描述
5.exe所在路径System.AppDomain.CurrentDomain.BaseDirectory
获取exe所在路径下的mailconfig.txt
string filepath=System.AppDomain.CurrentDomain.BaseDirectory + “mailconfig.txt”;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值