用c#操作txt文本文件的方法.docx

上传人:b****8 文档编号:9809586 上传时间:2023-05-21 格式:DOCX 页数:13 大小:17.61KB
下载 相关 举报
用c#操作txt文本文件的方法.docx_第1页
第1页 / 共13页
用c#操作txt文本文件的方法.docx_第2页
第2页 / 共13页
用c#操作txt文本文件的方法.docx_第3页
第3页 / 共13页
用c#操作txt文本文件的方法.docx_第4页
第4页 / 共13页
用c#操作txt文本文件的方法.docx_第5页
第5页 / 共13页
用c#操作txt文本文件的方法.docx_第6页
第6页 / 共13页
用c#操作txt文本文件的方法.docx_第7页
第7页 / 共13页
用c#操作txt文本文件的方法.docx_第8页
第8页 / 共13页
用c#操作txt文本文件的方法.docx_第9页
第9页 / 共13页
用c#操作txt文本文件的方法.docx_第10页
第10页 / 共13页
用c#操作txt文本文件的方法.docx_第11页
第11页 / 共13页
用c#操作txt文本文件的方法.docx_第12页
第12页 / 共13页
用c#操作txt文本文件的方法.docx_第13页
第13页 / 共13页
亲,该文档总共13页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

用c#操作txt文本文件的方法.docx

《用c#操作txt文本文件的方法.docx》由会员分享,可在线阅读,更多相关《用c#操作txt文本文件的方法.docx(13页珍藏版)》请在冰点文库上搜索。

用c#操作txt文本文件的方法.docx

用c#操作txt文本文件的方法

  如何读取文本文件内容:

  在本文介绍的程序中,是把读取的文本文件,用一个richTextBox组件显示出来。

要读取文本文件,必须使用到"StreamReader"类,这个类是由名字空间"System.IO"中定义的。

通过"StreamReader"类的"ReadLine()"方法,就可以读取打开数据流当前行的数据了。

下面代码实现的功能就是读取"C:

\file.txt"并在richTextBox1组件中显示出来:

  FileStreamfs=newFileStream("C:

\\file.txt",FileMode.Open,FileAccess.Read);

  StreamReaderm_streamReader=newStreamReader(fs);

  //使用StreamReader类来读取文件

  m_streamReader.BaseStream.Seek(0,SeekOrigin.Begin);

  //从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容

  this.richTextBox1.Text="";

  stringstrLine=m_streamReader.ReadLine();

  while(strLine!

=null)

  {

  this.richTextBox1.Text+=strLine+"\n";

  strLine=m_streamReader.ReadLine();

  }

  //关闭此StreamReader对象

  m_streamReader.Close();

  如何改变文本文件中数据内容:

  在本文介绍的程序中,改变文本文件数据内容的功能是通过改变richTextBox1中的内容来实现的,当richTextBox1这的内容改变后,按动"另存为",就把richTextBox1中内容存储到指定的文本文件中了。

要想改变文本文件内容,要使用到"StreamWriter"类,这个类和"StreamReader"一样,都是由"System.IO"名字空间来定义的。

通过"StreamWriter"类的"Write()"方法,就可以轻松实现文本文件内容的更改了。

下面代码的功能是:

如果"C"盘存在"file.txt",则把richTextBox1中的内容写入到"file.txt"中,如果不存在,则创建此文件,然后在写入文本数据。

  //创建一个文件流,用以写入或者创建一个StreamWriter

  FileStreamfs=newFileStream("C\\file.txt",FileMode.OpenOrCreate,FileAccess.Write);

  StreamWriterm_streamWriter=newStreamWriter(fs);

  m_streamWriter.Flush();

  //使用StreamWriter来往文件中写入内容

  m_streamWriter.BaseStream.Seek(0,SeekOrigin.Begin);

  //把richTextBox1中的内容写入文件

  m_streamWriter.Write(richTextBox1.Text);

  //关闭此文件

  m_streamWriter.Flush();

  m_streamWriter.Close();

  如何实现打印预览:

  打印预览是通过打印预览对话框来实现的,实现对读取得文本文件的打印预览,最为重要的就是要通知打印预览对话框所要预览的文件的内容。

下面代码就是把richTextBox1中显示的内容,通过打印预览对话框显示出来:

  //

  stringstrText=richTextBox1.Text;

  StringReadermyReader=newStringReader(strText);

  PrintPreviewDialogprintPreviewDialog1=newPrintPreviewDialog();

  printPreviewDialog1.Document=ThePrintDocument;

  printPreviewDialog1.FormBorderStyle=FormBorderStyle.Fixed3D;

  printPreviewDialog1.ShowDialog();

  下列代码是设定打印内容即打印richTextBox1中的内容:

  floatlinesPerPage=0;

  floatyPosition=0;

  intcount=0;

  floatleftMargin=ev.MarginBounds.Left;

  floattopMargin=ev.MarginBounds.Top;

  stringline=null;

  FontprintFont=richTextBox1.Font;

  SolidBrushmyBrush=newSolidBrush(Color.Black);

  //计算每一页打印多少行

  linesPerPage=ev.MarginBounds.Height/printFont.GetHeight(ev.Graphics);

  //重复使用StringReader对象,打印出richTextBox1中的所有内容

  while(count

=null))

  {

  //计算出要打印的下一行所基于页面的位置

  yPosition=topMargin+(count*printFont.GetHeight(ev.Graphics));

  //打印出richTextBox1中的下一行内容

  ev.Graphics.DrawString(line,printFont,myBrush,leftMargin,yPosition,newStringFormat());

  count++;

  }

  //判断如果还要下一页,则继续打印

  if(line!

=null)

  ev.HasMorePages=true;

  else

  ev.HasMorePages=false;

  myBrush.Dispose();

  usingSystem;

  usingSystem.Drawing;

  usingSystem.Collections;

  usingSystem.ComponentModel;

  usingSystem.Windows.Forms;

  usingSystem.Data;

  usingSystem.IO;

  usingSystem.Drawing.Printing;

  publicclassForm1:

Form

  {

  privateRichTextBoxrichTextBox1;

  privateButtonbutton1;

  privateButtonbutton2;

  privateButtonbutton3;

  privateButtonbutton4;

  privateButtonbutton5;

  privateOpenFileDialogopenFileDialog1;

  privateSaveFileDialogsaveFileDialog1;

  privatePrintDialogprintDialog1;

  privatePrintDocumentThePrintDocument;

  privatePrintPreviewDialogprintPreviewDialog1;

  privateStringReadermyReader;

  privateSystem.ComponentModel.Containercomponents=null;

  publicForm1()

  {

  //初始化窗体中的各个组件

  InitializeComponent();

  }

  //清除程序中使用多的资源

  protectedoverridevoidDispose(booldisposing)

  {

  if(disposing)

  {

  if(components!

=null)

  {

  components.Dispose();

  }

  }

  base.Dispose(disposing);

  }

  privatevoidInitializeComponent()

  {

  richTextBox1=newRichTextBox();

  button1=newButton();

  button2=newButton();

  button3=newButton();

  button4=newButton();

  button5=newButton();

  saveFileDialog1=newSaveFileDialog();

  openFileDialog1=newOpenFileDialog();

  printPreviewDialog1=newPrintPreviewDialog();

  printDialog1=newPrintDialog();

  ThePrintDocument=newPrintDocument();

  ThePrintDocument.PrintPage+=newPrintPageEventHandler(ThePrintDocument_PrintPage);

  SuspendLayout();

  richTextBox1.Anchor=AnchorStyles.None;

  richTextBox1.Name="richTextBox1";

  richTextBox1.Size=newSize(448,280);

  richTextBox1.TabIndex=0;

  richTextBox1.Text="";

  button1.Anchor=AnchorStyles.None;

  button1.Location=newPoint(41,289);

  button1.Name="button1";

  button1.Size=newSize(48,30);

  button1.TabIndex=1;

  button1.Text="打开";

  button1.Click+=newSystem.EventHandler(button1_Click);

  button2.Anchor=AnchorStyles.None;

  button2.Location=newPoint(274,288);

  button2.Name="button2";

  button2.Size=newSize(48,30);

  button2.TabIndex=4;

  button2.Text="预览";

  button2.Click+=newSystem.EventHandler(button2_Click);

  button3.Anchor=AnchorStyles.None;

  button3.Location=newPoint(108,288);

  button3.Name="button3";

  button3.Size=newSize(48,30);

  button3.TabIndex=2;

  button3.Text="保存";

  button3.Click+=newSystem.EventHandler(button3_Click);

  button4.Anchor=AnchorStyles.None;

  button4.Location=newPoint(174,288);

  button4.Name="button4";

  button4.Size=newSize(80,30);

  button4.TabIndex=3;

  button4.Text="打印机设置";

  button4.Click+=newSystem.EventHandler(button4_Click);

  button5.Anchor=AnchorStyles.None;

  button5.Location=newPoint(345,288);

  button5.Name="button5";

  button5.Size=newSize(48,30);

  button5.TabIndex=5;

  button5.Text="打印";

  button5.Click+=newSystem.EventHandler(button5_Click);

  saveFileDialog1.DefaultExt="*.txt";

  saveFileDialog1.FileName="file.txt";

  saveFileDialog1.InitialDirectory="c:

\\";

  saveFileDialog1.Title="另存为!

";

  openFileDialog1.DefaultExt="*.txt";

  openFileDialog1.FileName="file.txt";

  openFileDialog1.InitialDirectory="c:

\\";

  openFileDialog1.Title="打开文本文件!

";

  AutoScaleBaseSize=newSize(6,14);

  ClientSize=newSize(448,325);

  this.Controls.Add(button1);

  this.Controls.Add(button2);

  this.Controls.Add(button3);

  this.Controls.Add(button4);

  this.Controls.Add(button5);

  this.Controls.Add(richTextBox1);

  this.MaximizeBox=false;

  this.Name="Form1";

  this.Text="C#来操作文本文件";

  this.ResumeLayout(false);

  }

  staticvoidMain()

  {

  Application.Run(newForm1());

  }

  privatevoidbutton1_Click(objectsender,System.EventArgse)

  {

  try

  {

  if(openFileDialog1.ShowDialog()==DialogResult.OK)

  {

  FileStreamfs=newFileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read);

  StreamReaderm_streamReader=newStreamReader(fs);

  //使用StreamReader类来读取文件

  m_streamReader.BaseStream.Seek(0,SeekOrigin.Begin);

  //从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容

  this.richTextBox1.Text="";

  stringstrLine=m_streamReader.ReadLine();

  while(strLine!

=null)

  {

  this.richTextBox1.Text+=strLine+"\n";

  strLine=m_streamReader.ReadLine();

  }

  //关闭此StreamReader对象

  m_streamReader.Close();

  }

  }

  catch(Exceptionem)

  {

  Console.WriteLine(em.Message.ToString());

  }

  }

  privatevoidbutton3_Click(objectsender,System.EventArgse)

  {

  try

  {

  //获得另存为的文件名称

  if(saveFileDialog1.ShowDialog()==DialogResult.OK)

  {

  //创建一个文件流,用以写入或者创建一个StreamWriter

  FileStreamfs=newFileStream(@saveFileDialog1.FileName,FileMode.OpenOrCreate,FileAccess.Write);

  StreamWriterm_streamWriter=newStreamWriter(fs);

  m_streamWriter.Flush();

  //使用StreamWriter来往文件中写入内容

  m_streamWriter.BaseStream.Seek(0,SeekOrigin.Begin);

  //把richTextBox1中的内容写入文件

  m_streamWriter.Write(richTextBox1.Text);

  //关闭此文件

  m_streamWriter.Flush();

  m_streamWriter.Close();

  }

  }

  catch(Exceptionem)

  {

  Console.WriteLine(em.Message.ToString());

  }

  }

  privatevoidbutton4_Click(objectsender,System.EventArgse)

  {

  printDialog1.Document=ThePrintDocument;

  printDialog1.ShowDialog();

  }

  //预览打印文档

  privatevoidbutton2_Click(objectsender,System.EventArgse)

  {

  try

  {

  stringstrText=richTextBox1.Text;

  myReader=newStringReader(strText);

  PrintPreviewDialogprintPreviewDialog1=newPrintPreviewDialog();

  printPreviewDialog1.Document=ThePrintDocument;

  printPreviewDialog1.FormBorderStyle=FormBorderStyle.Fixed3D;

  printPreviewDialog1.ShowDialog();

  }

  catch(Exceptionexp)

  {

  Console.WriteLine(exp.Message.ToString());

  }

  }

  //打印richTextBox1中的内容

  privatevoidbutton5_Click(objectsender,System.EventArgse)

  {

  printDialog1.Document=ThePrintDocument;

  stringstrText=richTextBox1.Text;

  myReader=newStringReader(strText);

  if(printDialog1.ShowDialog()==DialogResult.OK)

  {

  ThePrintDocument.Print();

  }

  }

  protectedvoidThePrintDocument_PrintPage(objectsender,PrintPageEventArgsev)

  {

  floatlinesPerPage=0;

  floatyPosition=0;

  intcount=0;

  floatleftMargin=ev.MarginBounds.Left;

  floattopMargin=ev.MarginBounds.Top;

  stringline=null;

  FontprintFont=richTextBox1.Font;

  SolidBrushmyBrush=newSolidBrush(Color.Black);

  //计算每一页打印多少行

  linesPerPage=ev.MarginBounds.Height/printFont.

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2