当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现文本文件读写方法汇总

C#实现文本文件读写方法汇总

2019年07月18日  | 移动技术网IT编程  | 我要评论
方法一: using system; using system.collections.generic; using system.compo

方法一:

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();
    }
  }

以上所述就是本文的全部内容了,希望大家能够喜欢。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网