WPF 读写TxT文件
原文:WPF 读写TxT文件
文/嶽永鹏
WPF 中读取和写入TxT 是经常性的操作,本篇将从详细演示WPF如何读取和写入TxT文件。
首先,TxT文件希望逐行读取,并将每行读取到的数据作为一个数组的一个元素,因此需要引入List<string> 数据类型。且看代码:
public List<string> OpenTxt(TextBox tbx)
{
List<string> txt = new List<string>();
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "文本文件(*.txt)|*.txt|(*.rtf)|*.rtf";
if (openFile.ShowDialog() == true)
{
tbx.Text = "";
using (StreamReader sr = new StreamReader(openFile.FileName, Encoding.Default))
{
int lineCount = ;
while (sr.Peek() > )
{
lineCount++;
string temp = sr.ReadLine();
txt.Add(temp);
}
} }
return txt;
}
其中
using (StreamReader sr = new StreamReader(openFile.FileName, Encoding.Default))
{
int lineCount = ;
while (sr.Peek() > )
{
lineCount++;
string temp = sr.ReadLine();
txt.Add(temp);
}
10 }
StreamReader 是以流的方式逐行将TxT内容保存到List<string> txt中。
其次,对TxT文件的写入操作,也是将数组List<string> 中的每个元素逐行写入到TxT中,并保存为.txt文件。且看代码:
SaveFileDialog sf = new SaveFileDialog();
sf.Title = "Save text Files";
sf.DefaultExt = "txt";
sf.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
sf.FilterIndex = ;
sf.RestoreDirectory = true;
if ((bool)sf.ShowDialog())
{
using (FileStream fs = new FileStream(sf.FileName, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
{
for (int i = ; i < txt.Count; i++)
{
sw.WriteLine(txt[i]);
}
}
} }
而在这之中,相对于读入TxT文件相比,在写的时候,多用到了 FileStream类。
WPF 读写TxT文件的更多相关文章
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- java指定编码的按行读写txt文件(几种读写方式的比较)
转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- python操作txt文件中数据教程[1]-使用python读写txt文件
python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...
- C#读写txt文件的两种方法介绍[转]
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- C#读写txt文件的两种方法介绍 v
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- UNICODE环境下读写txt文件操作
内容转载自http://blog.sina.com.cn/s/blog_5d2bad130100t0x9.html UNICODE环境下读写txt文件操作 (2011-07-26 17:40:05) ...
- Wpf读写Xaml文件
前言 本文主要介绍Wpf读写Xaml文件. 读写实现 首先我们使用XamlWriter将Wpf的对象转换为Xaml字符串,代码如下: var btn = sender as Button; strin ...
- MFC读写.txt文件时进度条显示实时进度
整体实现方式:先获得文件长度,然后用每次读取的长度,计算出完成的百分比,用百分比的值设置进度条. 一.MFC进度条 Progress Control 相关函数 1. create() --创建Prog ...
随机推荐
- oracle 创建字段自增长——两种实现方式汇总(转)
mysql等其他数据库中有随着记录的插入而表ID自动增长的功能,而oracle却没有这样的功能,我们有以下两种方式可以解决字段自增长的功能. 因为两种方式都需要通过创建序列来实现,这里先给出序列的创建 ...
- solr/solrj原子更新
lucene原子更新自己不用多介绍,但solr它的包装,下面是一个简单的介绍是:这个操作是用于索引非常有用. 详细在代码中使用例如以下: /** * 原子更新方式 * */ public static ...
- ehCache浅谈(转)
ehcache FAQ中提到 Remember that a value in a cache element is globally accessible from multiple threads ...
- [Network]Transport Layer
1 Principles behind Transport Layer Services 1.1 Multiplexing/Demultiplexing Multiplexing at sender ...
- extjs每一个组件要设置唯一的ID
extjs每一个组件要设置唯一的ID,否则会造成各种错误 EXTJS基本上是靠ID来识别组件的,假如你在panel1中有个ID:"keyword"的textfield,而panel ...
- HDU 1074 Doing Homework(像缩进DP)
Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of h ...
- 王立平--android特权
//同意应用程序访问和更改checkin数据库"properties"数据表 android.permission.ACCESS_CHECKIN_PROPERTIES //同意应用 ...
- android控件 下拉刷新pulltorefresh
外国人写的下拉刷新控件,我把他下载下来放在网盘,有时候訪问不了github 支持各种控件下拉刷新 ListView.ViewPager.WevView.ExpandableListView.GridV ...
- java线程API学习 线程池ThreadPoolExecutor(转)
线程池ThreadPoolExecutor继承自ExecutorService.是jdk1.5加入的新特性,将提交执行的任务在内部线程池中的可用线程中执行. 构造函数 ThreadPoolExecut ...
- Mac OS X中报:java.io.UnixFileSystem.createFileExclusively(Native Method)的简单原因
这个博客太简单了!想到可能有其它朋友也遇到这个问题,就记录一下. 今天把一个之前在Windows上的Java项目放到Mac OS X上执行,本来认为应该非常easy的事情,结果还是报: Excepti ...