C#实现文本文件读写方法汇总
作者:hebedich 发布时间:2023-10-27 16:57:40
标签:C#,文本文件读写
方法一:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace txt
{
public partial class Form1 : Form
{
// string path;
public Form1()
{
InitializeComponent();
button3.Click+=button3_Click;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
string path1 = textBox2.Text;
if(!File.Exists(path1))
{
MessageBox.Show("文件不存在");
}
}
//浏览按钮
private void button3_Click(object sender, EventArgs e)
{
/*if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
string selUrl = folderBrowserDialog1.SelectedPath;
}*/
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox2.Text = ofd.FileName;
}
//选择文件夹
/* FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.ShowDialog();
MessageBox.Show(fbd.SelectedPath);
*/
}
//读取文件
private void button1_Click(object sender, EventArgs e)
{
if(textBox2.Text!=null)
//读取文件内容并显示在textbox1中让用户修改
{
string path=textBox2.Text;
if (File.Exists(path))
// {
// File.Delete(path);
// }
textBox1.Text = File.ReadAllText(path, Encoding.Default);
}
}
private void button2_Click(object sender, EventArgs e)
{
// string[] appendText=textBox1.Text;
File.WriteAllText(textBox2.Text, textBox1.Text, Encoding.Default);
MessageBox.Show("保存成功");
}
}
}
方法二:
namespace 文本文件打开测试
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Read_Click(object sender, EventArgs e)
{
//异常检测开始
try
{
FileStream fs = new FileStream(@tB_PachFileName.Text , FileMode.Open, FileAccess.Read);//读取文件设定
StreamReader m_streamReader = new StreamReader(fs, System.Text.Encoding.GetEncoding("GB2312"));//设定读写的编码
//使用StreamReader类来读取文件
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
// 从数据流中读取每一行,直到文件的最后一行,并在rTB_Display.Text中显示出内容
this.rTB_Display.Text = "";
string strLine = m_streamReader.ReadLine();
while (strLine != null)
{
this.rTB_Display.Text += strLine + "\n";
strLine = m_streamReader.ReadLine();
}
//关闭此StreamReader对象
m_streamReader.Close();
}
catch
{
//抛出异常
MessageBox.Show("指定文件不存在");
return;
}
//异常检测结束
}
private void btn_Replace_Click(object sender, EventArgs e)
{
//判断替换开始
if (tB_Replace.Text == ""&&tB_Replace_2.Text=="")
{
MessageBox.Show("想替换的字符都没有就换啊,你太有才了");
}
else
{
if (rTB_Display.Text == "")
{
MessageBox.Show("文件内容为空无法进行替换,请检查文件");
}
else
{
string str = rTB_Display.Text.ToString();
rTB_Display.Text = str.Replace(@tB_Replace.Text ,@tB_Replace_2.Text);//替换
}
}
//结束
}
private void btn_Save_Click(object sender, EventArgs e)
{
//异常检测开始
try
{
//创建一个文件流,用以写入或者创建一个StreamWriter
FileStream fs = new FileStream(@tB_Save.Text, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.Flush();
// 使用StreamWriter来往文件中写入内容
m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
// 把richTextBox1中的内容写入文件
m_streamWriter.Write(rTB_Display.Text);
//关闭此文件
m_streamWriter.Flush();
m_streamWriter.Close();
}
catch
{
//抛出异常
MessageBox.Show("写入文件失败,请检查路径 文件名与权限是否符合");
}
//异常检测结束
}
}
}
方法三:
//写入文本文件
class WriteTextFile
{
static void Main()
{
//如果文件不存在,则创建;存在则覆盖
//该方法写入字符数组换行显示
string[] lines = { "first line", "second line", "third line","第四行" };
System.IO.File.WriteAllLines(@"C:\testDir\test.txt", lines, Encoding.UTF8);
//如果文件不存在,则创建;存在则覆盖
string strTest = "该例子测试一个字符串写入文本文件。";
System.IO.File.WriteAllText(@"C:\testDir\test1.txt", strTest, Encoding.UTF8);
//在将文本写入文件前,处理文本行
//StreamWriter一个参数默认覆盖
//StreamWriter第二个参数为false覆盖现有文件,为true则把文本追加到文件末尾
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\testDir\test2.txt",true))
{
foreach (string line in lines)
{
if (!line.Contains("second"))
{
file.Write(line);//直接追加文件末尾,不换行
file.WriteLine(line);// 直接追加文件末尾,换行
}
}
}
}
}
//读取文本文件
class ReadTextFile
{
static void Main()
{
//直接读取出字符串
string text = System.IO.File.ReadAllText(@"C:\testDir\test1.txt");
Console.WriteLine(text);
//按行读取为字符串数组
string[] lines = System.IO.File.ReadAllLines(@"C:\testDir\test.txt");
foreach (string line in lines)
{
Console.WriteLine(line);
}
//从头到尾以流的方式读出文本文件
//该方法会一行一行读出文本
using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\testDir\test.txt"))
{
string str;
while ((str = sr.ReadLine()) != null)
{
Console.WriteLine(str);
}
}
Console.Read();
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。


猜你喜欢
- 存储访问框架,简称:SAF, 就是系统文件选择器+文件操作API。先选择文件,在用文件操作API处理文件。系统文件选择器,就和Windows
- 一对一查询一对一查询的模型用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户。一对一查询的需求:查询一个订单,与此同时查询
- 背景最近对于 Java 多线程做了一段时间的学习,笔者一直认为,学习东西就是要应用到实际的业务需求中的。否则要么无法深入理解,要么硬生生地套
- 在Android系统中提供了多种存储技术.通过这些存储技术可以将数据存储在各种存储介质上.比如sharedpreferences可以将数据保
- 递归是一个非常有用的知识点。写点实例帮助自己记忆中间有过程代码首先一个javapojo类package com.qcf.po;import
- 前言去年在公司参与了一个某某机场建设智能机场的一个项目,人脸登机是其中的一个功能模块,当时只是写了后台的接口,调用人脸识别设备的api,给闸
- 前言什么是AptAPT从原理上讲是一个编译期的注解处理工具(Annotation Processing Tool)。一些主流的三方库(But
- 本文为大家讲解了Struts2框架的入门知识,供大家参考,具体内容如下1、Struts2框架介绍Struts2框架是MVC流程框架,适合分层
- 下载UEditorhttps://ueditor.baidu.com/website/download.html下载完整源码和JSP版本Sp
- 1.相关概念Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性。建
- 使用ApkTool反编译Apk下载 apktool1.4.3.tar.bz2 、apktool-install-linux-r0
- 我最近在研究Spring框架的路上,那么今天也算个学习笔记吧!学习一下如何实现Bean的装配方法Bean的简介Java开发者一般会听过Jav
- SpringBoot 整合RabbitMq 自定义消息监听容器来实现消息批量处理前言RabbitMQ是一种常用的消息队列,Spring Bo
- 实现了一个有趣的小东西:使用自定义View绘图,一边画线,画出的线条渐渐变淡,直到消失。效果如下图所示:用属性动画或者渐变填充(Shader
- 一、下载https://www.eclipse.org/downloads/download.php?file=/oomph/epp/202
- webflux过滤器(RouterFunction实现)相关类与接口HandlerFiterFunction@FunctionalInter
- 直接用javaw.exe想打开aspectj-1.9.4.jar安装aspectJ选Java™ Platform SE binary提示JV
- 本文实例讲述了C#生成二维码的方法。分享给大家供大家参考。具体实现方法如下:首先引用ThoughtWorks.QRCode.dll具体代码如
- 上一篇介绍了Tesseract库的使用(OCR库Tesseract初探),文末提到了Tesseract是用c/c++开发的,也有C#的开源版
- 一、什么是iText?在企业的信息系统中,报表处理一直占比较重要的作用,iText是一种生成PDF报表的Java组件。通过在服务器端使用Js