1.添加命名空间

  System.IO;

  System.Text;

2.文件的读取

#region 读取TXT文本文件

        /// <summary>
/// FileStream读取文本文件
/// </summary>
public void FileStreamRead()
{
//文件路径
string filePath = AppDomain.CurrentDomain.BaseDirectory; // Server.MapPath("~/UploadFiles/"); //文件夹不存在则创建
if (!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
} filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Today.ToString("yyyMMdd") + ".txt";
if (System.IO.File.Exists(filePath))
{
//1.直接读取出字符串
string strText = System.IO.File.ReadAllText(filePath); //2.按行读取为字符串数组
string[] arrFileText = System.IO.File.ReadAllLines(filePath); //3.FileStream读取写入给定的缓存区
System.IO.FileStream fs = new FileStream(filePath, FileMode.Open);
fs.Seek(, SeekOrigin.Begin);
byte[] byData = new byte[];
fs.Read(byData, , );//byData传进来的字节数组,用以接受FileStream对象中的数据
System.Text.Decoder d = System.Text.Encoding.Default.GetDecoder();
char[] charData = new char[];
d.GetChars(byData, , byData.Length, charData, );
fs.Close();
} } /// <summary>
/// StreamReader读取文本文件
/// </summary>
public void StreamReaderRead()
{
//文件路径
string filePath = AppDomain.CurrentDomain.BaseDirectory; // Server.MapPath("~/UploadFiles/"); //文件夹不存在则创建
if (!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
} filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Today.ToString("yyyMMdd") + ".txt";
if (System.IO.File.Exists(filePath))
{
//1.从头到尾以流的方式读出文本文件,该方法会读出一行文本
System.IO.StreamReader sr = new StreamReader(filePath);
string strStreamReader = sr.ReadToEnd();
sr.Close();
}
} #endregion

文件读取

FileStream fs = new FileStream(@"c:\temp\ascii.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
//StreamReader sr3 = new StreamReader(fs);
//string str3 = sr3.ReadToEnd();
//sr3.Close();
StreamReader sr4 = new StreamReader(fs, System.Text.Encoding.Default);
string strCH = sr4.ReadToEnd();
sr4.Close();
string str2 = System.IO.File.ReadAllText(@"c:\temp\ascii.txt", System.Text.Encoding.ASCII);

3.文件的写入

#region 写入TXT文本文件

        /// <summary>
/// StreamWriter写入文本文件
/// </summary>
public void StreamWriterWrite()
{
//文件路径
string filePath = AppDomain.CurrentDomain.BaseDirectory; // Server.MapPath("~/UploadFiles/"); //文件不存在则创建
if (!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
}
filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Today.ToString("yyyMMdd") + ".txt"; #region 另一种方式
////FileMode.Append,FileAccess.Write追加文件
//FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);
//StreamWriter sw = new StreamWriter(fs); #endregion StreamWriter sw = new StreamWriter(filePath, true);
//Write直接追加文件末尾,不换行;WriteLine直接追加文件末尾,换行
sw.WriteLine("测试StreamWriter写入TXT文件" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")); //清空缓冲区、关闭流
sw.Flush();
sw.Close(); //直接追加到文件
//using (System.IO.StreamWriter sw = System.IO.File.AppendText(filePath))
//{
// sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
//} } /// <summary>
/// FileStream写入文本文件
/// </summary>
public void FileStreamWrite()
{
//文件路径
string filePath = AppDomain.CurrentDomain.BaseDirectory; // Server.MapPath("~/UploadFiles/"); //文件不存在则创建
if (!System.IO.Directory.Exists(filePath))
{
System.IO.Directory.CreateDirectory(filePath);
}
filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Today.ToString("yyyMMdd") + ".txt"; //1.该方法写入字符数组换行显示
string[] arrFile = { "first line", "second line", "third line", "第四行" };
System.IO.File.AppendAllLines(filePath, arrFile, System.Text.Encoding.Default); //2.字符串写入文本
string strTest = "该例子测试一个字符串写入文本文件。";
System.IO.File.AppendAllText(filePath, strTest, System.Text.Encoding.Default); //3.FileMode.Append,FileAccess.Write追加文件
FileStream fs = new FileStream(filePath, FileMode.Append,FileAccess.Write);
byte[] data = System.Text.Encoding.Default.GetBytes("测试FileStream写入TXT文件" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
fs.Write(data, , data.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
} #endregion

文件写入

#region 读取 保存

                ////读取
//string strFileTxt = string.Empty;
//using (FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
//{
// StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8); //选择编码方式
// strFileTxt = sr.ReadToEnd();
//} ////保存
//using (FileStream fs = new FileStream(strSavePath, FileMode.Create, FileAccess.Write))
//{
// byte[] fData = Encoding.UTF8.GetBytes(strFileTxt);
// fs.Write(fData, 0, fData.Length);
// fs.Flush();
//} #endregion

读取 保存

http://www.cnblogs.com/jx270/archive/2013/04/14/3020456.html

C#读写txt文件的方法的更多相关文章

  1. [转载]C#读写txt文件的两种方法介绍

    C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...

  2. C#读写txt文件的两种方法介绍

    C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...

  3. C#读写txt文件的两种方法介绍[转]

    C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...

  4. C#读写txt文件的两种方法介绍 v

    C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...

  5. Javascript写入txt和读取txt文件的方法

    文章主要介绍了Javascript写入txt和读取txt文件的方法,需要的朋友可以参考下1. 写入 FileSystemObject可以将文件翻译成文件流. 第一步: 例: 复制代码 代码如下: Va ...

  6. java指定编码的按行读写txt文件(几种读写方式的比较)

    转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976  版权声明:本文为博主原创文章,未经博主允许不得转载. ...

  7. python使用xlrd模块读写Excel文件的方法

    本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...

  8. WPF 读写TxT文件

    原文:WPF 读写TxT文件 文/嶽永鹏 WPF 中读取和写入TxT 是经常性的操作,本篇将从详细演示WPF如何读取和写入TxT文件. 首先,TxT文件希望逐行读取,并将每行读取到的数据作为一个数组的 ...

  9. python操作txt文件中数据教程[1]-使用python读写txt文件

    python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...

随机推荐

  1. tableView 的协议方法

    需遵守协议 UITableViewDataSource, UITableViewDelegate,并设置代理 UITableViewDelegate 继承自 UIScrollViewDelegate ...

  2. UIViewController函数调用顺序

    /*********** 0 执行1次而已 ******************/ + (void)load { NSLog(@" 0:%s", __func__); } /*** ...

  3. 从理论认识J2EE

    前言 在学习J2EE这块,看了成套的视频,感觉,感觉,感觉收获不是特别大,没用马老师讲得好,但是多少还是和J2EE打了个招呼,比如J2EE著名的十三个规范,他们有的人说不算什么规范,顶多可以理解为十三 ...

  4. 51nod1453(排列组合)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1453 题意: 中文题诶~ 思路: 因为最后一个球总是在编号比 ...

  5. Ceph配置文件查看修改方式

    1.     修改ceph配置文件的方式有三种:(其中包含临时和永久生效) 1)      修改所有或者指定的进程 2)      修改当前服务器进程 3)      修改配置文件 Note:在线修改 ...

  6. linux 远程连接ssh提示 IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY解决

    Linux ssh 远程登录到其他机器上时,有时会出现登不进去,并弹出如下类似提示的情况: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  7. P4094 [HEOI2016/TJOI2016]字符串 后缀数组+主席树+二分答案

    $ \color{#0066ff}{ 题目描述 }$ 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了一个长为n的字符串s,和m个问题.佳媛姐姐必须 ...

  8. P2117 小Z的矩阵

    题意: 给你一个初始01矩阵 三种操作 1.给一个x,把第x行01互换 2.给一个x,把第x列01互换 3.求$(\sum_{i=1}^n\sum_{j=1}^nf[i][j]*f[j][i])%2$ ...

  9. fatal error C1859: “Release\IWBServer.pch”意外的预编译头错误,只需重新运行编译器就可能修复此问题

    解决方案 1.    创建预编译头(/Yc)   -- >     stdafx.cpp    使用预编译头(/Yu) 2.    complie 3.    使用预编译头(/Yu)    -- ...

  10. python中各种转义字符

    转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \’ 单引号 \” 双引号 \a 响铃 \b 退格(Backspace) \e 转义 \000 空 \n 换行 \v 纵向制表符 \t 横向制 ...