C#實(shí)現(xiàn)文本文件讀寫(xiě)方法匯總
更新時(shí)間:2015年06月17日 11:26:23 投稿:hebedich
本文給大家匯總介紹了C#實(shí)現(xiàn)文本文件讀寫(xiě)的方法,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。
方法一:
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)
//讀取文件內(nèi)容并顯示在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 文本文件打開(kāi)測(cè)試
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Read_Click(object sender, EventArgs e)
{
//異常檢測(cè)開(kāi)始
try
{
FileStream fs = new FileStream(@tB_PachFileName.Text , FileMode.Open, FileAccess.Read);//讀取文件設(shè)定
StreamReader m_streamReader = new StreamReader(fs, System.Text.Encoding.GetEncoding("GB2312"));//設(shè)定讀寫(xiě)的編碼
//使用StreamReader類(lèi)來(lái)讀取文件
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
// 從數(shù)據(jù)流中讀取每一行,直到文件的最后一行,并在rTB_Display.Text中顯示出內(nèi)容
this.rTB_Display.Text = "";
string strLine = m_streamReader.ReadLine();
while (strLine != null)
{
this.rTB_Display.Text += strLine + "\n";
strLine = m_streamReader.ReadLine();
}
//關(guān)閉此StreamReader對(duì)象
m_streamReader.Close();
}
catch
{
//拋出異常
MessageBox.Show("指定文件不存在");
return;
}
//異常檢測(cè)結(jié)束
}
private void btn_Replace_Click(object sender, EventArgs e)
{
//判斷替換開(kāi)始
if (tB_Replace.Text == ""&&tB_Replace_2.Text=="")
{
MessageBox.Show("想替換的字符都沒(méi)有就換啊,你太有才了");
}
else
{
if (rTB_Display.Text == "")
{
MessageBox.Show("文件內(nèi)容為空無(wú)法進(jìn)行替換,請(qǐng)檢查文件");
}
else
{
string str = rTB_Display.Text.ToString();
rTB_Display.Text = str.Replace(@tB_Replace.Text ,@tB_Replace_2.Text);//替換
}
}
//結(jié)束
}
private void btn_Save_Click(object sender, EventArgs e)
{
//異常檢測(cè)開(kāi)始
try
{
//創(chuàng)建一個(gè)文件流,用以寫(xiě)入或者創(chuàng)建一個(gè)StreamWriter
FileStream fs = new FileStream(@tB_Save.Text, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.Flush();
// 使用StreamWriter來(lái)往文件中寫(xiě)入內(nèi)容
m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
// 把richTextBox1中的內(nèi)容寫(xiě)入文件
m_streamWriter.Write(rTB_Display.Text);
//關(guān)閉此文件
m_streamWriter.Flush();
m_streamWriter.Close();
}
catch
{
//拋出異常
MessageBox.Show("寫(xiě)入文件失敗,請(qǐng)檢查路徑 文件名與權(quán)限是否符合");
}
//異常檢測(cè)結(jié)束
}
}
}
方法三:
//寫(xiě)入文本文件
class WriteTextFile
{
static void Main()
{
//如果文件不存在,則創(chuàng)建;存在則覆蓋
//該方法寫(xiě)入字符數(shù)組換行顯示
string[] lines = { "first line", "second line", "third line","第四行" };
System.IO.File.WriteAllLines(@"C:\testDir\test.txt", lines, Encoding.UTF8);
//如果文件不存在,則創(chuàng)建;存在則覆蓋
string strTest = "該例子測(cè)試一個(gè)字符串寫(xiě)入文本文件。";
System.IO.File.WriteAllText(@"C:\testDir\test1.txt", strTest, Encoding.UTF8);
//在將文本寫(xiě)入文件前,處理文本行
//StreamWriter一個(gè)參數(shù)默認(rèn)覆蓋
//StreamWriter第二個(gè)參數(shù)為false覆蓋現(xiàn)有文件,為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);
//按行讀取為字符串?dāng)?shù)組
string[] lines = System.IO.File.ReadAllLines(@"C:\testDir\test.txt");
foreach (string line in lines)
{
Console.WriteLine(line);
}
//從頭到尾以流的方式讀出文本文件
//該方法會(huì)一行一行讀出文本
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();
}
}
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
基于C#的socket編程的TCP異步的實(shí)現(xiàn)代碼
本篇文章主要介紹了基于C#的socket編程的TCP異步的實(shí)現(xiàn)代碼,詳解的講訴了TCP通信異步的實(shí)現(xiàn),有興趣的可以了解一下。2016-11-11
WinForm實(shí)現(xiàn)按名稱(chēng)遞歸查找控件的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)按名稱(chēng)遞歸查找控件的方法,需要的朋友可以參考下2014-08-08
automation服務(wù)器不能創(chuàng)建對(duì)象 解決方法
本文主要介紹如何解決“automation服務(wù)器不能創(chuàng)建對(duì)象”錯(cuò)誤,從而解決Visual Studio.Net不能正常使用的問(wèn)題,需要的朋友可以參考下。2016-06-06
C#中使用@聲明變量示例(逐字標(biāo)識(shí)符)
這篇文章主要介紹了C#中使用@聲明變量示例(逐字標(biāo)識(shí)符)在C#中,@符號(hào)不僅可以加在字符串常量之前,使字符串不作轉(zhuǎn)義之用,還可以加在變量名之前,使變量名與關(guān)鍵字不沖突,這種用法稱(chēng)為“逐字標(biāo)識(shí)符”,需要的朋友可以參考下2015-06-06
C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器小程序
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01

