C#读写txt文件的方法
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文件的方法的更多相关文章
- [转载]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数组,然后输出 ...
- 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数组,然后输出 ...
- Javascript写入txt和读取txt文件的方法
文章主要介绍了Javascript写入txt和读取txt文件的方法,需要的朋友可以参考下1. 写入 FileSystemObject可以将文件翻译成文件流. 第一步: 例: 复制代码 代码如下: Va ...
- java指定编码的按行读写txt文件(几种读写方式的比较)
转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- python使用xlrd模块读写Excel文件的方法
本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...
- WPF 读写TxT文件
原文:WPF 读写TxT文件 文/嶽永鹏 WPF 中读取和写入TxT 是经常性的操作,本篇将从详细演示WPF如何读取和写入TxT文件. 首先,TxT文件希望逐行读取,并将每行读取到的数据作为一个数组的 ...
- python操作txt文件中数据教程[1]-使用python读写txt文件
python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...
随机推荐
- uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型
在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型,不过C语言(nesc是C的扩展)里面好像没有这种数据类型啊!怎么又是u又是_t的?很多人有这样的 ...
- 51nod1455(dp)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1455 题意: 中文题诶~ 思路: dp 1 <= n, ...
- Python flask虚拟环境安装
1.安装virtualenv 2.在当前路径下创建文件夹,启动虚拟环境 3.在使用虚拟环境前需激活,前面出现(env说明在虚拟环境中).虚拟环境中默认安装了pip,所以直接pip安装flask 4.在 ...
- vue原理实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 用Decorator控制Koa路由
在Spring中Controller长这样 @Controller public class HelloController{ @RequestMapping("/hello") ...
- flink学习笔记-快速生成Flink项目
说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...
- Number BZOJ3275 最大流
有N个正整数,需要从中选出一些数,使这些数的和最大. 若两个数a,b同时满足以下条件,则a,b不能同时被选 1:存在正整数C,使a*a+b*b=c*c 2:gcd(a,b)=1 Sample Outp ...
- CF431C k-Tree dp
Quite recently a creative student Lesha had a lecture on trees. After the lecture Lesha was inspired ...
- Java中Array与ArrayList的主要区别
1)精辟阐述: 可以将 ArrayList想象成一种"会自动扩增容量的Array". 2)Array([]):最高效:但是其容量固定且无法动态改变: ArrayList: ...
- [例] 用MappedByteBuffer更新文件内容
import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; impor ...